Skip to content

perf(lexer): opener-state checkpoint - fold passes resume from the bare { (rubash#281) - #486

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/reader-relex
Oct 10, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/reader-relex

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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 explicit tail_resume flag — the first cut got this wrong and perf10_token_reuse_tests caught 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:

phase time
whole tokenize 418 ms
— gate scans (comsub/quotes/compound/param, all checkpoint-driven) 32 ms
— full tokenize_plain passes: 4281 passes / 445 KB 294 ms
—— of which 19 passes ≥ 4 KB re-lexed 270 KB (the fold-completion re-lexes) —
— boundary tail-resumes: 353 6 ms
parse 90 ms
noexec exec 0.5 ms

After 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

scenario base (master) this PR note
nvm.sh -n, re-lexed bytes (debug counters) 445 KB / 4281 full passes 267 KB / 4767 passes full passes = one-per-logical-line floor; 46 opener resumes replace the fold re-lexes
nvm.sh -n wall, release build, interleaved A/B best-of 156–175 ms 153–197 ms within host noise — see honesty note
nvm.sh -n wall, debug build ~585 ms ~575 ms within noise
vs GNU bash 5.3.0 (-n 19 ms) — ~8x release / ~31x debug harness release ratio is single-digit

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 -n gap 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 (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 (env-dependent families: bashdb, tilde/HOME, examples, heredoc-fd).
  • A/B vs a master-built binary, byte-identical stdout/stderr/rc: brace-group matrices (nested groups, }-bearing strings/comments/case patterns, multiline strings, ${ joins, unclosed-group EOF diagnostics with physical line numbers), plus full nvm.sh -n.
  • cargo fmt clean; 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).

… 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
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.

[perf] Incremental reader deep system: batch tokenizer re-lexes O(buffer) per appended line (nvm -n 720x / OpenSSH -n timeout residual)

1 participant