Repository navigation
Splice blink token stream in place to remove O(n^2) tail copies - #187
Merged
Merged
Conversation
TomasVotruba
force-pushed
the
perf-token-stream
branch
from
October 9, 2026 09:57
cfcd923 to
8b47382
Compare
TomasVotruba
enabled auto-merge (squash)
October 9, 2026 11:42
TomasVotruba
disabled auto-merge
October 9, 2026 13:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
blink's token-stream mutations were super-linear.
BenchmarkFixPipeline(lex + all rules + render) showed throughput dropping as file size grew, the signature of an O(n^2) cost. The culprit wasblink/internal/tokens/stream.go:ReplaceRange(start, end, repl)allocated a fresh copy of the entire tail on every call, then rebuilt the slice - even when the replacement was the same length as the range it replaced (the common case, e.g. reordering/normalising modifier keywords).InsertAt/RemoveAtshifted the whole tail by hand.These are called from ~90 sites across the rules, many inside loops over a single file, so repeated single-element edits were quadratic. A memory profile at 256 classes attributed 78% of all allocations to
ReplaceRangealone (1.66 GB over the benchmark run).Change
Replace the hand-rolled splices with the stdlib in-place operations:
ReplaceRange->slices.Replace- no tail copy; whenlen(repl)equals the range it overwrites in place and the tail never moves.InsertAt->slices.InsertRemoveAt->slices.DeleteThe public
StreamAPI is unchanged (same signatures), so all 300+ rules and their tests are untouched. Output is byte-identical - the existing golden and parity suites cover this.Benchmark (AMD Ryzen AI 9 HX 370, go1.26)
At 256 classes: 2.67x faster and ~39x less allocated memory.
Remaining work (not in this PR)
A re-profile after the fix shows the residual cost is now
memmovefrom genuinely single-elementInsertAt/RemoveAtcalls scattered across ~15 rules, each still shifting the tail once. Removing that last bit of super-linearity would need either a gap buffer for the token slice (adds index translation to the hotAtpath and carries real byte-parity risk across all rules) or retrofitting those rules to a batched edit API. Both are more invasive and better handled separately; this PR is the simplest safe change that already removes the dominant quadratic allocation cost.