Collect reports in a difference list

Using a difference list we get O(1) appending vs. O(n) with the built-in
list type. Overall for n reports this results in O(n) operations instead
of O(n^2) operations.
This commit is contained in:
Janek Spaderna 2022-06-26 23:58:37 +02:00
parent d1e552c09a
commit f4863dd3e8
3 changed files with 15 additions and 7 deletions

View File

@ -1,6 +1,6 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.6.
-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack
@ -67,6 +67,7 @@ library
build-depends:
base >=4.7 && <5
, data-default >=0.7 && <1
, dlist ==1.0.*
, hashable >=1.3 && <2
, prettyprinter >=1.7.0 && <2
, prettyprinter-ansi-terminal >=1.1.0 && <2
@ -111,6 +112,7 @@ test-suite diagnose-megaparsec-tests
base >=4.7 && <5
, data-default >=0.7 && <1
, diagnose
, dlist ==1.0.*
, hashable >=1.3 && <2
, prettyprinter >=1.7.0 && <2
, prettyprinter-ansi-terminal >=1.1.0 && <2
@ -150,6 +152,7 @@ test-suite diagnose-parsec-tests
base >=4.7 && <5
, data-default >=0.7 && <1
, diagnose
, dlist ==1.0.*
, hashable >=1.3 && <2
, prettyprinter >=1.7.0 && <2
, prettyprinter-ansi-terminal >=1.1.0 && <2
@ -188,6 +191,7 @@ test-suite diagnose-rendering-tests
base >=4.7 && <5
, data-default >=0.7 && <1
, diagnose
, dlist ==1.0.*
, hashable >=1.3 && <2
, prettyprinter >=1.7.0 && <2
, prettyprinter-ansi-terminal >=1.1.0 && <2

View File

@ -8,11 +8,12 @@ category: "Error Reporting"
dependencies:
- base >= 4.7 && < 5
- data-default >= 0.7 && < 1
- dlist >= 1.0 && < 1.1
- hashable >= 1.3 && < 2
- prettyprinter >= 1.7.0 && < 2
- prettyprinter-ansi-terminal >= 1.1.0 && < 2
- unordered-containers >= 0.2 && < 0.3
- hashable >= 1.3 && < 2
- data-default >= 0.7 && < 1
- wcwidth >= 0.0.1 && <1
- text >= 1.0.0.0 && <= 2.0
# ^^^ This is unfortunately required, but as 'prettyprinter' already depends on it, it will already have been fetched

View File

@ -20,8 +20,11 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Aeson (ToJSON(..), encode, object, (.=))
import Data.ByteString.Lazy (ByteString)
#endif
import Data.DList (DList)
import qualified Data.DList as DL
import Data.Default (Default, def)
import Data.Foldable (fold)
import Data.Foldable (fold, toList)
import Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as HashMap
import Data.List (intersperse)
@ -38,7 +41,7 @@ import System.IO (Handle)
-- to create a new empty diagnostic, and 'addFile' and 'addReport' to alter its internal state.
data Diagnostic msg
= Diagnostic
[Report msg]
(DList (Report msg))
-- ^ All the reports contained in a diagnostic.
--
-- Reports are output one by one, without connections in between.
@ -85,7 +88,7 @@ prettyDiagnostic ::
Diagnostic msg ->
Doc Annotation
prettyDiagnostic withUnicode tabSize (Diagnostic reports file) =
fold . intersperse hardline $ prettyReport file withUnicode tabSize <$> reports
fold . intersperse hardline $ prettyReport file withUnicode tabSize <$> toList reports
{-# INLINE prettyDiagnostic #-}
-- | Prints a 'Diagnostic' onto a specific 'Handle'.
@ -127,7 +130,7 @@ addReport ::
Report msg ->
Diagnostic msg
addReport (Diagnostic reports files) report =
Diagnostic (reports <> [report]) files
Diagnostic (reports `DL.snoc` report) files
{-# INLINE addReport #-}
#ifdef USE_AESON