Change group not to produce a Union when the document cannot be flattened#116
Change group not to produce a Union when the document cannot be flattened#116
Conversation
Addresses one idea from #112.
This addresses #112 (comment).
|
Previously: > diag $ group $ "x" <> hardline
Union
( Cat ( Char 'x' ) Fail )
( Cat ( Char 'x' ) Line )Now: Cat ( Char 'x' ) Line |
|
|
|
It seems that we'd no longer produce |
Nope. |
If |
At least |
| -- 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) |
There was a problem hiding this comment.
Maybe this would be a better name:
| 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 |
| | AlreadyFlat | ||
| -- ^ The input was already flat, e.g. a 'Text'. | ||
| | NeverFlat | ||
| -- ^ The input couldn't be flattened: It contained a 'Line' or 'Fail'. |
There was a problem hiding this comment.
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.
|
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. |
|
This would also fix the issue of repeated |
|
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 |
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. |
And here's the corresponding profile (see #99 (comment) for the original):
|
This should speed up some pretty-printing tasks. See haskell-prettyprinter/prettyprinter#116 (comment) for some numbers.
This should speed up some pretty-printing tasks. See haskell-prettyprinter/prettyprinter#116 (comment) for some numbers.
This should speed up some pretty-printing tasks. See haskell-prettyprinter/prettyprinter#116 (comment) for some numbers.
Fixes #99, fixes #112.