Repository navigation
perf(lexer): opener-state checkpoint - fold passes resume from the bare { (rubash#281) - #486
Merged
Merged
Conversation
… from the bare { instead of re-lexing the whole buffer (rubash#281)
The batch tokenizer's complete-command-boundary checkpoint (d8fbaa3) made
every pass over an accumulating logical line lex only the appended tail -
except the FOLD pass: the pass whose appended line completes a brace group
refused the resume (a resumed lexer never re-visits the opener before its
checkpoint offset, and scanner.rs's '{' arm folds the group into ONE token
exactly there), so it re-lexed the WHOLE accumulated buffer from byte 0.
nvm.sh -n: 19 fold-completion passes re-lexed 270 KB of the 445 KB total
re-lexed bytes (measured pass-size histogram, debug build).
Fix: capture an opener-state checkpoint at the bare '{' emission itself
(scanner.rs '{' arm, unclosed-group path; boundary_state() at the emission
point is the between-token state JUST BEFORE the opener). On a pass the
boundary checkpoint cannot serve (every fold pass, and any pass whose
boundary was dropped by a non-append mutation while the group stayed open),
the stored prefix tokens plus a lex resumed from the opener offset with the
pre-'{' state reproduce the full pass byte for byte by the same
deterministic-scanner argument as the boundary resume (parse.y:3557
read_token is a streaming reader and never re-lexes consumed text) -
including the fold itself, which the re-visited '{' arm now completes over
the group's whole appended-to text. Cost class: a fold pass pays the
group's own text once, not every byte accumulated before the opener.
Same invalidation discipline as the boundary checkpoint: every non-append
mutation of logical_line drops it (rotation / backslash pop / IFS_GLUE
insert / commit sites), as do posix and extglob gate flips; tail-resumed
passes do not detach it (the opener sits before their resume offset and a
later fold still needs it); the finish() flush uses it the same way.
Summary-fold semantics: a tail-resumed pass folds only the token delta; an
opener-resumed pass rewrites the list from the fold point, so the
keyword-stack summary refolds from the line-start snapshot (the list is
byte-identical to a fresh full pass either way - the tail/opener
distinction is the tail_resume flag, fixed after the first cut folded
deltas onto a summary that still carried the open group's push; caught by
perf10_token_reuse_tests).
Verification: cargo test --lib 630 passed / 2 failed = the pre-existing
host-env failures (pwd logical-mode, path virtual-system-root), failure set
byte-identical to master; full cargo test --no-fail-fast 4294 passed /
70 failed = failure set identical to master; A/B vs a master binary:
brace-group matrices (nested groups, '}'-bearing strings, comments and
case patterns, multiline strings, '${' joins, unclosed-group EOF
diagnostics) byte-identical; nvm.sh -n stdout/stderr/rc byte-identical.
Closes #281
This was referenced Oct 10, 2026
Merged
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.
Closes #281.
What this lands
The last named cost center of the #281 umbrella: the fold-completion full re-lex of the batch tokenizer's incremental reader.
The complete-command-boundary checkpoint (d8fbaa3, perf3) makes every pass over an accumulating logical line lex only the appended tail — except the pass whose appended line completes a
{group. That pass refused the resume (a resumed lexer never re-visits an opener before its checkpoint offset, and scanner.rs's{arm folds the whole group into ONE token exactly there), so it re-lexed the entire accumulated buffer from byte 0, once per group close.Fix: capture an opener-state checkpoint at the bare-
{emission itself (scanner.rs{arm, unclosed-group path): the byte offset plus the between-token lexer state as of JUST BEFORE the opener. On a fold pass (and any pass whose boundary checkpoint was dropped while the group stayed open), the stored prefix tokens plus a lex resumed from the opener offset reproduce the full pass byte for byte by the same deterministic-scanner argument as the boundary resume (GNU parse.y:3557 read_token is a streaming reader and never re-lexes consumed text) — including the fold itself, which the re-visited{arm completes over the group's whole appended-to text. A fold pass now pays the group's own text once, not every byte accumulated before the opener.Same invalidation discipline as the boundary checkpoint (non-append mutations, posix/extglob flips, commit), tail-resumed passes never detach it, and the
finish()flush uses it the same way. Summary-fold semantics: tail resume folds the token delta, opener resume refolds the keyword-stack summary from the line-start snapshot (both lists are byte-identical to a fresh full pass; the distinction is an explicittail_resumeflag — the first cut got this wrong andperf10_token_reuse_testscaught it).Attribution (per the #437 methodology: measure first)
Scratch phase timers over
rubash -n benchmarks/corpus/nvm.sh(172 KB, 5226 physical lines), debug build, current master:tokenize_plainpasses: 4281 passes / 445 KBAfter this change: full-pass bytes re-lexed 445 KB → 267 KB (−40%), with 46 opener-resumes replacing the fold/ungated full passes (tail resumes unchanged at 353; full passes drop to the one-per-logical-line floor).
Before/after
nvm.sh -n, re-lexed bytes (debug counters)nvm.sh -nwall, release build, interleaved A/B best-ofnvm.sh -nwall, debug build-n19 ms)Honesty note: the fold re-lexes' wall-clock share was smaller than their byte share (the group-body fold scan is a cheap linear walk; the per-logical-line lex floor and the parse phase dominate both builds), so this lands the last structural O(buffer)-per-append cost of #281's reader and the byte-level reduction, but the remaining nvm
-ngap to GNU is the per-token lex floor plus the parse phase (90 ms debug / ~30 ms release on this host) — a separate token-architecture/parser lane, out of this PR's scope.Verification
cargo test --lib: 630 passed / 2 failed = the pre-existing host-env failures (pwdlogical-mode,pathvirtual-system-root), failure set byte-identical to master.cargo test --no-fail-fast: 4294 passed / 70 failed, failure set identical to master (env-dependent families: bashdb, tilde/HOME, examples, heredoc-fd).}-bearing strings/comments/case patterns, multiline strings,${joins, unclosed-group EOF diagnostics with physical line numbers), plus fullnvm.sh -n.cargo fmtclean; built warning-free.Closes #281 (umbrella items all landed: walker
${-arm memo + boundary checkpoint (perf3), brace_scan zero-copy (perf3), continuation.rs slice fix (15f94b9), lextok admission gates, and this fold-completion opener checkpoint).