Skip to content

Alternative fix for #205#283

Draft
sjakobi wants to merge 2 commits intomasterfrom
sjakobi/issue205-codex
Draft

Alternative fix for #205#283
sjakobi wants to merge 2 commits intomasterfrom
sjakobi/issue205-codex

Conversation

@sjakobi
Copy link
Copy Markdown
Collaborator

@sjakobi sjakobi commented Mar 30, 2026

I asked Codex to come up with an alternative to #282 for fixing #205.

I haven't looked at it very closely myself yet.

Notes from Codex:

• Findings

  - #205 is not caused by the strict Int in SLine at prettyprinter/src/Prettyprinter/
    Internal.hs:1669. The real problem is the eager case x of ... in the Line branch at
    prettyprinter/src/Prettyprinter/Internal.hs:2076, because that forces the tail stream
    while layoutSmart is still exploring alternatives.
  - That also explains why only layoutSmart blows up: its fits predicate keeps walking across
    multiple SLines at prettyprinter/src/Prettyprinter/Internal.hs:1995, while layoutPretty
    stops at the first line break.
  - I checked the old sjakobi/issue205 work:
      - a61681e = “Remove bang on Int in SLine”
      - ee48764 = revert of that change
      - 2a18909 = full revert of #139
  - Bench result with the exact issue reproducer, built -O2 with ghc-9.12.2:
      - current master: n=30 -> 8.55s, n=33 -> timed out at 20s
      - lazy-SLine variant: n=30 -> 8.66s, n=33 -> timed out at 20s
      - full revert of #139: effectively instant
  - So the “make the Int lazy” idea is dead: same asymptotics, same runtime.

  Best Candidate

  I prototyped a targeted fix in /tmp/pp-master-detached/prettyprinter/src/Prettyprinter/
  Internal.hs:1981 without touching your main checkout:

  - build layoutSmart with plain SLine i x again
  - add a linear post-pass dropIndentationOnEmptyLines that only rewrites SLine i x to SLine 0
    x when the transformed tail starts with SEmpty or another SLine
  - keep layoutPretty and layoutUnbounded on the current behavior
  - in layoutSmart.fits, only normalize blank-line indentation in the branch that was already
    going to recurse

  That prototype:

  - fixes #205 immediately: n=30, 33, 40 all measured 0.00s
  - preserves the #139 behavior
  - avoids the overreach of removeTrailingWhitespace postprocessing
  - passes the full testsuite via cabal test prettyprinter:testsuite --with-compiler=ghc-9.12

  Other Options

  - Full revert of #139: simplest, but reintroduces the old trailing-indentation regression.
  - removeTrailingWhitespace after layoutSmart: fast and easy, but too aggressive; it strips
    user-produced trailing spaces too.
  - New SimpleDocStream constructor for deferred indentation: probably the cleanest long-term
    design, but much more invasive.

sjakobi added 2 commits March 30, 2026 17:21
When the timeout limit is raised, the test finishes after ~8s on my
laptop.
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.

1 participant