Skip to content

perf(lexer): parse-throughput construction wins - fold-before-push, gated leading_ws capture, TokStr reader state (rubash#241) - #554

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/parse-241
Oct 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/parse-241

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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 on nvm.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)

  1. Feeder folds before pushing and moves the token (emit_line_separator, heredoc-close arm of push_main_line): the Token clones existed only because fold_token ran before the push. fold_token reads the token and the summary stacks, never output, so the reorder is observationally identical — three String copies per committed line erased.
  2. leading_ws gap capture admitted on evidence (nvm.sh -n: 5166 capture passes -> 2243, ~13k allocs): literal ((/[[/case bytes in the accumulated logical line, an open esac on the fold keyword_stack, or in_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 in record_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. The keyword_stack arm is load-bearing: an adversarial probe (alias-borne case + 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 put case bytes 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.
  3. LexerParseState reader values become TokStr (new src/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 as str, UTF-8 by construction — same discipline the raw bytes already enjoy; String conversion 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)

probe master (ms) this PR (ms) delta
23-nvm-parse-n (session 1 / session 2) 144.8 / 131.4 134.9 / 125.8 -6.9% / -4.3%
20-as-fn-mkdir-p-rep40 41.9 36.6 -12.5%
17-parse-flat8000-n 11.7 11.2 -4.4%
18-nested-brace-nst1-d200 30.1 29.3 -2.5%
21-configure-head1374-n 34.3 33.6 -2.0%
16-parse-flat8000 451.4 443.6 -1.7%

Honesty note

Verification

  • Full cargo test --no-fail-fast vs a fresh origin/master build: 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 -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 (second full run: sets identical).
  • 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 (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.

…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.
@caomengxuan666
caomengxuan666 merged commit db3218a into master Oct 12, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the perf/parse-241 branch October 13, 2026 02:00
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant