Repository navigation
perf(lexer): parse-throughput construction wins - fold-before-push, gated leading_ws capture, TokStr reader state (rubash#241) - #554
Merged
Conversation
…ush, gated leading_ws capture, TokStr reader state (rubash#241) Attribution (temporary process-allocator + phase probes over the release build, all probe code removed before landing): the process allocator was ~75-80% of the `nvm.sh -n` wall - 459k allocations for the 173KB script on master, dominated by per-token Strings, per-pass reader-state clones and the per-line leading_ws capture. With this change the same probe counts 331k (the shipped tree carries no probe code). Three construction-only, zero-semantic-change wins: * feeder folds before pushing and moves the token: emit_line_separator (and the heredoc-close push in push_main_line) cloned each Token only because fold_token ran first; fold_token reads the token and the summary stacks, never `output`, so the fold order is observationally identical and the per-line clone (three String copies per committed line) is gone. * leading_ws gap capture is admitted on evidence that a consumer can see it: literal `((` / `[[` / `case` bytes in the accumulated logical line, an open `esac` on the fold keyword_stack, or the PST_CONDCMD port's in_cond_command. All five read sites (arithmetic raw text, for ((;;)) parts, the case offending-line echo, `[[ ]]` body/regexp fragments, the lexer's own =~ RHS end decision) read tokens inside a construct whose opener is provably present in one of those signals at capture time; substring hits over-capture, which is identity. The keyword_stack arm is load-bearing: an adversarial probe (alias-borne `case` + rubash#131 indented offending-line echo) caught the literal-bytes-only gate losing the four-space indent on master- observable output; with the arm the echo is byte-identical again. nvm.sh -n: 5166 capture passes -> 2243, ~13k allocations erased. * LexerParseState last_token/token_before_that store values as TokStr (new src/token_str.rs): the reader state is cloned up to five times per feeder pass (restore + take + line mirror + boundary/opener snapshots), and every reader compares short literals, so the inline arm (<= 22 bytes, 32-byte footprint, no allocation) makes those copies allocation-free. Value bytes are unchanged: TokStr derefs, compares and hashes as str, and converts to String at the AST boundary exactly as before (UTF-8 by construction, same discipline the raw bytes already enjoy). ~30k of the 459k master allocations. Interleaved A/B (native-parent timing, base and branch alternate each round on the same host under concurrent agent builds, release builds of master vs this tree, medians): 23-nvm-parse-n 144.8 -> 134.9ms (-6.9%) and 131.4 -> 125.8ms (-4.3%) in two sessions; 20-as-fn-mkdir-p-rep40 41.9 -> 36.6ms (-12.5%); 17-parse-flat8000-n 11.7 -> 11.2ms (-4.4%); 18-nested-brace-nst1-d200 -2.5%; 21-configure-head1374-n -2.0%; 16-parse-flat8000 -1.7%. 24-nvm-load TIMEOUTs identically on master and this tree under the current host environment (rc=124 parity; pre- existing, excluded from claims). Verification: - Full `cargo test --no-fail-fast` vs a fresh origin/master build: failure sets byte-identical, bidirectional diff EMPTY (69 failed / 4365 passed on both sides; pre-existing host-env families: bashdb, pwd logical-mode, heredoc, path virtual-system-root). One job-control test (wait -n prefers a completed job) failed once in the first branch run and passes 8/8 standalone on BOTH builds - timing-flaky, not a parity break. - 8 semantic + adversarial probe scripts over every touched surface (leading_ws consumers, alias-borne case/[[/(( incl. multi-line bodies and multi-space error echoes, =~ fragment splits, heredoc/separator folding): stdout+stderr+rc byte-identical to master. - cargo fmt clean.
This was referenced Oct 12, 2026
caomengxuan666
added a commit
that referenced
this pull request
Oct 13, 2026
…601) 40 commits (PRs #496-#600) folded into [1.4.0] by theme instead of per-commit listing: - GNU semantics waves 1-4b with their issue families - argv re-expansion P0 pair (#511/#555), PWD POSIX persona (#537) - -s on-demand stdin driver as the canonical library reader (#586) - CLI entry-shape semantics (#589-#591) and the GNU usage contract (#585) - diagnostic message fidelity (#551) and parser diagnostic tokens - Windows path contract (#568/#592) - lexer/executor performance rounds (#554/#562/#501) - docs: boundary invariants, platform gap, baselines, debugger M0 Commit message deliberately carries no release/version claim.
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.
Continues #241 (P2, parse-throughput residue; lane rebased onto current master incl. #496-#502). Follows the #501 playbook: profiled attribution -> construction-only fixes with pinned safety arguments -> interleaved A/B -> full-suite failure-set parity. Not merged by this agent; leaving review/merge to the captain.
Method
Temporary attribution probes over the release build (counting global allocator + per-phase ns/alloc timers,
RUBASH_PERF_PROBE=1), all probe code removed before landing — the shipped diff carries none of it. Headline finding onnvm.sh -n(173KB script): the process allocator is ~75-80% of the wall — 459k allocations on master (previous instance's figure, reproduced; a never-free bump-allocator A/B bounded allocator+free share at ~120ms of ~150ms). With this change the same probe counts 331k.Fixes (one commit, each site's safety argument pinned in-code)
emit_line_separator, heredoc-close arm ofpush_main_line): theTokenclones existed only becausefold_tokenran before the push.fold_tokenreads the token and the summary stacks, neveroutput, so the reorder is observationally identical — three String copies per committed line erased.leading_wsgap capture admitted on evidence (nvm.sh -n: 5166 capture passes -> 2243, ~13k allocs): literal((/[[/casebytes in the accumulated logical line, an openesacon the foldkeyword_stack, orin_cond_command(PST_CONDCMD port). All five read sites (arithmetic raw text,for ((;;))parts, the case offending-line echo,[[ ]]body/regexp fragments, the lexer's own=~RHS end decision inrecord_token) read tokens inside a construct whose opener provably arms one of those signals at capture time; substring hits over-capture, which is identity for behavior. Thekeyword_stackarm is load-bearing: an adversarial probe (alias-bornecase+ the rubash#131 indented offending-line echo) caught the literal-bytes-only gate losing the four-space indent on master-observable output — per-token alias expansion does not putcasebytes in the logical line, but the fold sees the expanded Keyword. With the arm, byte-identical again. Alias-borne((has no feeder-visible state; the four-shape adversarial matrix (multi-line alias((bodies, arith-error raw echoes with multi-space gaps) is byte-identical with capture disarmed — recorded in-code as empirically pinned.LexerParseStatereader values becomeTokStr(newsrc/token_str.rs, ~30k allocs): the reader state is cloned up to five times per feeder pass (restore + take + line mirror + boundary/opener snapshots) and every reader compares short literals. Inline arm: <= 22 bytes in place, 32-byte footprint, no allocation; value bytes unchanged (deref/eq/hash asstr, UTF-8 by construction — same discipline therawbytes already enjoy;Stringconversion at the AST boundary exactly as before).Before/after (release, same host, interleaved A/B — base and branch alternate each round under concurrent agent builds; native Python parent times the child directly, no MSYS fork tax; medians)
Honesty note
Verification
cargo test --no-fail-fastvs a freshorigin/masterbuild: failure sets byte-identical, bidirectional diff EMPTY (4365 passed / 69 failed on both sides; the pre-existing host-env families: bashdb, pwd logical-mode, heredoc, path virtual-system-root). One job-control test (wait -nprefers a completed job) failed once in the first branch run and passes 8/8 standalone on BOTH builds — timing-flaky, not a parity break (second full run: sets identical).case/[[/((incl. multi-line bodies and multi-space error echoes,=~fragment splits, heredoc/separator folding): stdout+stderr+rc byte-identical to master.cargo fmtclean (pre-commit hook enforced); diff is 4 files:src/lexer/mod.rs,src/lexer/scanner.rs,src/lib.rs,src/token_str.rs.Closes #241? No — the issue's remaining items (configure-full -n super-linearity, nvm-load hang triage, the #281 reader family) stay open; this lands the construction-only slice the attribution supports.