Skip to content

Change group not to produce a Union when the document cannot be flattened#116

Merged
sjakobi merged 8 commits intomasterfrom
112-group-faster-fail
Jan 22, 2020
Merged

Change group not to produce a Union when the document cannot be flattened#116
sjakobi merged 8 commits intomasterfrom
112-group-faster-fail

Conversation

@sjakobi
Copy link
Collaborator

@sjakobi sjakobi commented Jan 21, 2020

Fixes #99, fixes #112.

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

Previously:

> diag $ group $ "x" <> hardline
Union 
    ( Cat ( Char 'x' ) Fail ) 
    ( Cat ( Char 'x' ) Line )

Now:

Cat ( Char 'x' ) Line

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

dhall's tasty testsuite still passes when built with this branch.

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

It seems that we'd no longer produce Fails at all! We might as well remove the constructor then?!

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

It seems that we'd no longer produce Fails at all!

Nope. flatten still returns Fail for Line. Maybe we can get rid of that though…

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

flatten still returns Fail for Line. Maybe we can get rid of that though…

If flatten would also produce a FlattenResult, I think that probably reduce laziness, probably in a bad way?!

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

If flatten would also produce a FlattenResult, I think that probably reduce laziness, probably in a bad way?!

At least layoutCompact, which doesn't care about a first Union alternative at all, would suffer

https://github.com/quchen/prettyprinter/blob/ff555e19b7b17a74f16e6fb062256a57dabe4d92/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs#L1858

@sjakobi sjakobi changed the title WIP: Change group to reveal failing Union alternatives more prominently WIP: Change group not to produce a Union when the document cannot be flattened Jan 21, 2020
-- if the document is static (e.g. contains only a plain 'Empty' node). See
-- [Group: special flattening] for further explanations.
changesUponFlattening :: Doc ann -> Maybe (Doc ann)
changesUponFlattening :: Doc ann -> FlattenResult (Doc ann)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be a better name:

Suggested change
changesUponFlattening :: Doc ann -> FlattenResult (Doc ann)
tryFlatten :: Doc ann -> FlattenResult (Doc ann)

Union x _ -> changesUponFlattening x <|> Just x
FlatAlt _ y -> Flattened (flatten y)
Line -> NeverFlat
Union x _ -> Flattened x
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also includes #100 / fixes #99, mostly because I couldn't be bothered to write an Alternative instance.

| AlreadyFlat
-- ^ The input was already flat, e.g. a 'Text'.
| NeverFlat
-- ^ The input couldn't be flattened: It contained a 'Line' or 'Fail'.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main idea of this PR is to represent the old Nothing result with two constructors: One to indicate that the result is already flat enough, the other to indicate that flattening is impossible, and which allows shortcuts.

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

This speeds up the case described in dhall-lang/dhall-haskell#1496 (comment) from ~11.9s to ~9.2s for me.

I'm pretty confident that this is a good change at this stage.

@sjakobi sjakobi changed the title WIP: Change group not to produce a Union when the document cannot be flattened Change group not to produce a Union when the document cannot be flattened Jan 21, 2020
@sjakobi sjakobi requested a review from quchen January 21, 2020 04:15
@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

This would also fix the issue of repeated groups I mentioned in #100 (comment).

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

I wonder whether I should add some kind of test for this. This patch should be unobservable after rendering, so I guess I could use diag

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 21, 2020

This would also fix the issue of repeated groups I mentioned in #100 (comment).

Oops, nope:

> diag $ group . group . group $ "x" <> line
Union 
    ( Cat ( Char 'x' ) ( Char ' ' ) ) 
    ( Union 
        ( Cat ( Char 'x' ) ( Char ' ' ) ) 
        ( Union 
            ( Cat ( Char 'x' ) ( Char ' ' ) ) 
            ( Cat ( Char 'x' ) 
                ( FlatAlt Line ( Char ' ' ) )
            )
        )
    )

EDIT: I have opened #120 to track this.

@sjakobi
Copy link
Collaborator Author

sjakobi commented Jan 22, 2020

This speeds up the case described in dhall-lang/dhall-haskell#1496 (comment) from ~11.9s to ~9.2s for me.

And here's the corresponding profile (see #99 (comment) for the original):

	Wed Jan 22 15:15 2020 Time and Allocation Profiling Report  (Final)

	   dhall +RTS -p -RTS type --file /home/simon/src/dhall-kubernetes-charts/stable/jenkins/index.dhall --explain

	total time  =       14.08 secs   (14079 ticks @ 1000 us, 1 processor)
	total alloc = 36,050,760,208 bytes  (excludes profiling overheads)

COST CENTRE               MODULE                                             SRC                                                                         %time %alloc

getDecodeAction           Codec.CBOR.Decoding                                src/Codec/CBOR/Decoding.hs:311:1-55                                          51.0   65.6
prettyCharacterSet        Dhall.Pretty.Internal                              src/Dhall/Pretty/Internal.hs:(512,1)-(1177,62)                                5.6    3.4
layoutWadlerLeijen        Data.Text.Prettyprint.Doc.Internal                 src/Data/Text/Prettyprint/Doc/Internal.hs:(1775,1)-(1843,60)                  5.5    3.0
unorderedTraverseWithKey_ Dhall.Map                                          src/Dhall/Map.hs:(647,1)-(648,65)                                             5.3    5.1
escapeLabel               Dhall.Pretty.Internal                              src/Dhall/Pretty/Internal.hs:(428,1)-(433,34)                                 4.0    2.1
renderLazy                Data.Text.Prettyprint.Doc.Render.Terminal.Internal src/Data/Text/Prettyprint/Doc/Render/Terminal/Internal.hs:(108,1)-(148,60)    3.7    3.7
removeTrailingWhitespace  Data.Text.Prettyprint.Doc.Internal                 src/Data/Text/Prettyprint/Doc/Internal.hs:(1477,1)-(1537,119)                 2.3    1.1
hashExpression            Dhall.Import                                       src/Dhall/Import.hs:(1133,1)-(1134,67)                                        2.2    2.4
styleToRawText            Data.Text.Prettyprint.Doc.Render.Terminal.Internal src/Data/Text/Prettyprint/Doc/Render/Terminal/Internal.hs:(278,1)-(304,29)    1.8    2.0
deserialiseIncremental    Codec.CBOR.Read                                    src/Codec/CBOR/Read.hs:(165,1)-(167,46)                                       1.7    1.1
layoutSmart               Data.Text.Prettyprint.Doc.Internal                 src/Data/Text/Prettyprint/Doc/Internal.hs:(1741,1)-(1767,59)                  1.6    0.2
changesUponFlattening     Data.Text.Prettyprint.Doc.Internal                 src/Data/Text/Prettyprint/Doc/Internal.hs:(567,1)-(607,21)                    1.5    1.1
csi                       System.Console.ANSI.Codes                          src/System/Console/ANSI/Codes.hs:77:1-75                                      1.4    2.7
toBuilder                 Codec.CBOR.Write                                   src/Codec/CBOR/Write.hs:(84,1)-(85,57)                                        1.0    1.0

  • time: 1.5% (down from 19.3%)
  • allocations: 1.1% (down from 22.6%)

@sjakobi sjakobi merged commit 3eacd62 into master Jan 22, 2020
@sjakobi sjakobi deleted the 112-group-faster-fail branch January 22, 2020 16:16
sjakobi added a commit to dhall-lang/dhall-haskell that referenced this pull request Jan 22, 2020
This should speed up some pretty-printing tasks. See

haskell-prettyprinter/prettyprinter#116 (comment)

for some numbers.
sjakobi added a commit to dhall-lang/dhall-haskell that referenced this pull request Jan 22, 2020
This should speed up some pretty-printing tasks. See

haskell-prettyprinter/prettyprinter#116 (comment)

for some numbers.
sjakobi added a commit to dhall-lang/dhall-haskell that referenced this pull request Jan 22, 2020
This should speed up some pretty-printing tasks. See

haskell-prettyprinter/prettyprinter#116 (comment)

for some numbers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make group Fail faster changesUponFlattening can be slow

1 participant