Skip to content

Commit

Permalink
Speed up prettyprinter's renderLazy (#166)
Browse files Browse the repository at this point in the history
renderSimplyDecorated's keeping track of the annotation stack wasn't
actually necessary here. With the new custom render loop, we speed up
the large-output benchmarks by roughly 10%.

Note that this also removes some error detection functionality for
SimpleDocStreams with unbalanced SAnnPush and SAnnPop constructors.

Fixes #165.
  • Loading branch information
sjakobi authored Jun 13, 2020
1 parent a03a2bf commit 58c140a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions prettyprinter/src/Data/Text/Prettyprint/Doc/Render/Text.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

#include "version-compatibility-macros.h"
Expand Down Expand Up @@ -26,7 +27,6 @@ import System.IO
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Internal
import Data.Text.Prettyprint.Doc.Render.Util.Panic
import Data.Text.Prettyprint.Doc.Render.Util.StackMachine

#if !(SEMIGROUP_IN_BASE)
import Data.Semigroup
Expand Down Expand Up @@ -56,7 +56,16 @@ import Control.Applicative
-- (foo bar)
-- sit amet
renderLazy :: SimpleDocStream ann -> TL.Text
renderLazy = TLB.toLazyText . renderSimplyDecorated TLB.fromText (pure mempty) (pure mempty)
renderLazy = TLB.toLazyText . go
where
go = \case
SFail -> panicUncaughtFail
SEmpty -> mempty
SChar c rest -> TLB.singleton c <> go rest
SText _l t rest -> TLB.fromText t <> go rest
SLine i rest -> TLB.singleton '\n' <> (TLB.fromText (textSpaces i) <> go rest)
SAnnPush _ann rest -> go rest
SAnnPop rest -> go rest

-- | @('renderStrict' sdoc)@ takes the output @sdoc@ from a rendering function
-- and transforms it to strict text.
Expand Down

0 comments on commit 58c140a

Please sign in to comment.