Skip to content

perf(html): stream tokens line by line instead of materialising all lines - #1528

Closed
TheNetherWatcher wants to merge 1 commit into
alecthomas:masterfrom
TheNetherWatcher:perf/mem-html-stream-lines
Closed

TheNetherWatcher wants to merge 1 commit into
alecthomas:masterfrom
TheNetherWatcher:perf/mem-html-stream-lines

Conversation

@TheNetherWatcher

Copy link
Copy Markdown

The HTML formatter collects every token into a slice (slices.Collect(iterator) in Format), then writeHTML calls chroma.SplitTokensIntoLines, which builds a second copy of all tokens grouped by line. Both stay alive for the whole render even though output is written one line at a time. On a 1 MB Go file lexing alone peaks around 4.8 MB, while formatting peaks around 27 MB.

This PR:

  • adds a small forEachLine helper that splits lines exactly like SplitTokensIntoLines (including dropping the empty trailing line) but reuses one line buffer instead of materialising every line
  • without line numbers (the default, and what Hugo uses unless line numbers are enabled), Format streams tokens straight from the lexer and never builds the token slice
  • with line numbers, tokens are still collected because the total line count determines the number padding, but lines are counted rather than copied
  • writeHTML takes the token sequence and the line count; no public API change

Results

Peak live heap on real Go source, median of 5:

Options Input Before After
default (no line numbers) 64 KB 2.86 MB 1.72 MB (−40%)
default 256 KB 7.23 MB 2.39 MB (−67%)
default 1 MB 26.98 MB 4.85 MB (−82%)
classes + line numbers 1 MB 27.03 MB 24.84 MB (−8%)

Runtime is unchanged within noise (−1.6% geomean, 1 MB default 633 → 611 ms). Bytes allocated drop 10–14% without line numbers and 5–6% with them.

Correctness

  • Added a test checking forEachLine against SplitTokensIntoLines.
  • Compared HTML output against master for every lexer testdata file, a set of edge cases, and 3,000 random token streams across 11 option combinations: all 36,113 outputs are byte-identical.
  • go test ./... and golangci-lint run ./formatters/html/ pass.

One behaviour difference: without line numbers, if a lexer panics mid-stream, the writer may already have received partial HTML before the error is returned (previously nothing was written). The error is still returned, and the terminal formatters already behave this way.

The SVG formatter has the same collect-then-split pattern; happy to follow up there if this looks good.

@alecthomas

Copy link
Copy Markdown
Owner

Thanks for this, and for the careful measurements and output-equivalence testing. They made the trade-offs easy to evaluate.

I've landed a similar change in 9d60314 that takes the same approach (stream without line numbers, buffer only when the line count is needed), but does it by changing chroma.SplitTokensIntoLines itself to take and return iterators, rather than adding a separate line splitter to the HTML formatter. That keeps a single implementation of the splitting logic, and also lets the SVG formatter drop its extra token copy.

I compared both branches on a 1 MB Go file. Without line numbers, peak heap is the same for both, down to the cost of lexing alone. This PR is a little ahead elsewhere: about 5% fewer total allocations from reusing the line buffer, and about 2.5 MB lower peak with line numbers. I decided the single, iterator-based implementation was worth that small difference.

Closing in favour of that commit, but thanks again for digging into this.

@alecthomas alecthomas closed this Nov 22, 2026
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.

2 participants