Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pattern SString #214

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ packages: prettyprinter
, prettyprinter-compat-annotated-wl-pprint
tests: true
benchmarks: true

source-repository-package
type: git
location: https://github.com/Bodigrim/linear-builder
28 changes: 12 additions & 16 deletions prettyprinter/bench/LargeOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ randomProgram seed size = let MkGen gen = arbitrary in gen (mkQCGen seed) size
main :: IO ()
main = do
let prog = randomProgram 1 60
renderedProg = (renderLazy . layoutPretty defaultLayoutOptions { layoutPageWidth = Unbounded } . pretty) prog
(progLines, progWidth) = let l = TL.lines renderedProg in (length l, maximum (map TL.length l))
renderedProg = (renderStrict . layoutPretty defaultLayoutOptions { layoutPageWidth = Unbounded } . pretty) prog
(progLines, progWidth) = let l = T.lines renderedProg in (length l, maximum (map T.length l))
putDoc ("Program size:" <+> pretty progLines <+> "lines, maximum width:" <+> pretty progWidth)

let renderWith :: (Doc ann -> SimpleDocStream ann) -> Program -> TL.Text
renderWith f = renderLazy . f . pretty
let renderWith :: (Doc ann -> SimpleDocStream ann) -> Program -> T.Text
renderWith f = renderStrict . f . pretty

let _80ColumnsLayoutOptions = defaultLayoutOptions { layoutPageWidth = AvailablePerLine 80 0.5 }
unboundedLayoutOptions = defaultLayoutOptions { layoutPageWidth = Unbounded }
Expand All @@ -193,17 +193,13 @@ main = do

defaultMain
[ bgroup "80 characters, 50% ribbon"
[ bgroup "prettyprinter"
[ bench "layoutPretty" (nf (renderWith (layoutPretty _80ColumnsLayoutOptions)) prog)
, bench "layoutSmart" (nf (renderWith (layoutSmart _80ColumnsLayoutOptions)) prog)
, bench "layoutCompact" (nf (renderWith layoutCompact ) prog)
]
, bench "ansi-wl-pprint" (nf (($ "") . WL.displayS . WL.renderPretty 0.5 80 . WL.pretty) prog) ]
[ bench "layoutPretty" (nf (renderWith (layoutPretty _80ColumnsLayoutOptions)) prog)
, bench "layoutSmart" (nf (renderWith (layoutSmart _80ColumnsLayoutOptions)) prog)
, bench "layoutCompact" (nf (renderWith layoutCompact ) prog)
]
, bgroup "Infinite/large page width"
[ bgroup "prettyprinter"
[ bench "layoutPretty" (nf (renderWith (layoutPretty unboundedLayoutOptions)) prog)
, bench "layoutSmart" (nf (renderWith (layoutSmart unboundedLayoutOptions)) prog)
, bench "layoutCompact" (nf (renderWith layoutCompact ) prog)
]
, bench "ansi-wl-pprint" (nf (($ "") . WL.displayS . WL.renderPretty 1 (fromIntegral progWidth + 10) . WL.pretty) prog) ]
[ bench "layoutPretty" (nf (renderWith (layoutPretty unboundedLayoutOptions)) prog)
, bench "layoutSmart" (nf (renderWith (layoutSmart unboundedLayoutOptions)) prog)
, bench "layoutCompact" (nf (renderWith layoutCompact ) prog)
]
]
5 changes: 4 additions & 1 deletion prettyprinter/prettyprinter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ library
, ScopedTypeVariables

build-depends:
base >= 4.5 && < 5
base >= 4.5 && < 5,
text-builder-linear

if flag(text)
build-depends: text >= 1.2
Expand Down Expand Up @@ -170,6 +171,7 @@ test-suite testsuite


benchmark fusion
buildable: False
type: exitcode-stdio-1.0
hs-source-dirs: bench
main-is: Fusion.hs
Expand All @@ -191,6 +193,7 @@ benchmark fusion
buildable: False

benchmark faster-unsafe-text
buildable: False
build-depends:
base >= 4.5 && < 5
, prettyprinter
Expand Down
19 changes: 16 additions & 3 deletions prettyprinter/src/Prettyprinter/Render/Text.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE UnicodeSyntax #-}
{-# LANGUAGE BangPatterns #-}

#include "version-compatibility-macros.h"

Expand Down Expand Up @@ -36,6 +39,8 @@ import Data.Semigroup
import Control.Applicative
#endif

import Data.Text.Builder.Linear.Buffer

-- $setup
--
-- (Definitions for the doctests)
Expand Down Expand Up @@ -70,9 +75,17 @@ renderLazy = TLB.toLazyText . go
-- | @('renderStrict' sdoc)@ takes the output @sdoc@ from a rendering function
-- and transforms it to strict text.
renderStrict :: SimpleDocStream ann -> Text
renderStrict = TL.toStrict . renderLazy


renderStrict sdc = runBuffer (\b -> (go b sdc))
where
go :: Buffer ⊸ SimpleDocStream ann -> Buffer
go !b !sbc = case sbc of
SFail -> undefined b
SEmpty -> b
SChar c rest -> go (b |>. c) rest
SText _l t rest -> go (b |> t) rest
SLine i rest -> go ((b |>. '\n') |>… i) rest
SAnnPush _ann rest -> go b rest
SAnnPop rest -> go b rest

-- | @('renderIO' h sdoc)@ writes @sdoc@ to the file @h@.
--
Expand Down