Skip to content

perf(executor): dispatch-floor residue - shape-only cd/pwd comsub admission, DEBUG-trap parse-once cache (rubash#375) - #562

Merged
caomengxuan666 merged 2 commits into
masterfrom
perf/dispatch-floor
Oct 12, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
perf/dispatch-floor

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Continues #375 (after #484's single-handle cut). Follows the #501/#554 playbook: profiled attribution -> construction-only fixes with pinned equivalence -> interleaved A/B -> full-suite failure-set parity. Not merged by this agent; leaving review/merge to the captain. Two commits, one per fix; every temporary attribution probe was removed before landing (the shipped diff carries none of it).

Attribution (release build; RUBASH_EXEC_PROFILE phase timers, all removed; samply/ETW sampling is dead on this host — 11-14 samples per run — so attribution ran on phase decomposition + targeted standalone probes)

finding where the time went
$(cd X && pwd) paid the canonical round trip TWICE the admission probe (command_substitution_uses_specialized_path) ran the full cd/pwd helper (expand_word + shell_path_to_windows + single-handle canonical_physical_dir: CreateFileW+GetFileInformationByHandle+GetFinalPathNameByHandleW, ~99µs in-process) and discarded the result; the real expansion paid it again
DEBUG trap re-parsed its action every firing run_debug_trap splice+tokenize+parse = 12.5µs x 6002 firings (75ms of 4.0s) in the bats_debug_trap shape
"CreateFileW handle-table scale effect" is disproven standalone Win32 probe (20k rounds): CreateFileW 13-19µs + GetFileInformationByHandle 5-10µs + GetFinalPathNameByHandleW 31-46µs + CloseHandle 7-10µs; holding 0/100/1k/10k/50k extra handles changed nothing (65-82µs flat, noise-bound). The in-process premium over the old "isolated 36µs" figure was measuring CreateFileW ALONE vs the whole round; no scale effect exists
generic no-trap dispatch floor is small isolated probes: chain 0.6µs/cmd, empty 2.9µs/cmd, total floor ~12-16µs/cmd incl. ~7µs/iter loop machinery; the #375 bats-context 56µs/46µs figures were trap-firing work (this PR cuts most of the remainder)

Fixes (one commit each)

  1. Shape-only cd/pwd admission — the shared command_substitution_cd_pwd_shape predicate replaces the full-probe admission. Pinned equivalence: the helper returns Some for EVERY shape-matching body (both failure arms yield Some(String::new()) + status 1), so admission is decidable from word shape alone. Side bonus toward GNU: the removed probe pass expanded the cd target (and wrote the comsub status) before the real pass — a side-effecting target $(cd $(echo n >> cnt; echo d) && pwd) now appends ONCE (WSL GNU 5.3.0 probe: lines=1; master appended twice).
  2. DEBUG-trap parse-once cache — key = alias-spliced action text, value = parsed AST in a RefCell; per firing the AST is taken out, line-stamped (the existing unconditional stamp walk), executed, put back. Stable text = zero clones, zero parses per firing (GNU parses once at trap invocation, trap.c read_trap_action). Re-entrancy guards sit before the cache; RETURN trap untouched.

Before/after (release, same host, interleaved rounds, base = fresh origin/master build; median/min)

probe master (ms) this PR (ms) delta
bats_debug_trap shape (trap + fn + $(cd . && pwd) + 2 array copies) x2000 1769 1259 -28.8% / -29.3%
$(cd X && pwd) x2000 plain loop 360 222 -38.4% / -36.9%
$(cd X && pwd) x5000 1224 848 -30.7% / -36.3%
$(cd X && pwd) x5000 in function 1312 936 -28.7% / -33.9%
noop : x20k (no-trap canary) 182 184 +0.3~0.8% (mins equal, flat)
2x special-array copy x10k (canary) 725 746 +3.0% median / +0.2% min (flat)

Honesty note: what is NOT fixed, and why

Verification

  • Full cargo test --no-fail-fast on this branch vs a fresh origin/master build: failure sets bidirectionally identical by name (55 failures on BOTH sides; 170 suites parsed, per-suite (result, passed, failed) counts identical; totals 4432 passed / 55 failed each — the known host-env families).
  • 21-shape semantic battery over both touched surfaces (cd/pwd comsub: normal/missing/file-not-dir/bare-cd/../trailing-slash/semicolon/pwd -P/pwd -L/rejected shapes/side-effect-once/status propagation/xtrace disqualification/nested/quoted/exotic; DEBUG trap: LINENO/BASH_COMMAND, loop+function firings, re-set inside action, alias text evolution after arming, functrace inheritance, ERR/RETURN adjacency, same-text re-arm, nested comsub in action, exit-2 terminal skip): stdout+stderr+rc byte-identical to master except the single intended diff (side-effecting cd target now expands once, GNU-anchored).
  • cargo fmt clean; diff is 5 files, +92/-53.

…(rubash#375)

Attribution (temporary phase timers over the release build, removed
before landing): $(cd X && pwd) paid the single-handle canonical round
trip (CreateFileW + GetFileInformationByHandle + GetFinalPathNameByHandleW,
~99us in-process) TWICE per substitution — the admission probe
(command_substitution_uses_specialized_path) ran the full helper via
command_substitution_cd_pwd_output_quiet_probe and discarded the result,
then the real expansion paid the whole round trip again.

Admission never needed any of it: the helper returns Some for EVERY body
passing its shape gate (both failure arms yield Some(String::new()) with
status 1), so the probe reduces exactly to the shape predicate. The
predicate is now one shared function (command_substitution_cd_pwd_shape)
used by both the admission and the real expansion — single source of
truth, no divergence possible.

Construction-only, pinned equivalence:
- admission set unchanged (same split functions, same shape conditions,
  independent of expansion results);
- the removed probe pass also expanded the cd target and wrote
  last_command_substitution_status BEFORE the real pass — GNU expands the
  cd target exactly once at cd execution (subst.c), and a side-effecting
  target ($(cd $(echo n >> cnt; echo d) && pwd)) now appends once where
  master appended twice; WSL GNU Bash 5.3.0 probe: lines=1, matching the
  new behavior;
- cd failure diagnostics still print exactly once (the quiet-probe
  suppression existed only to mask the double run).

A/B (interleaved rounds, same host, release builds of db3218a vs this
tree, medians/mins): $(cd X && pwd) x2000 -34.2%/-38.4%; x5000 plain
loop -30.6~-34.7%/-36.3%; x5000 inside function -28.7~-33.5%/-33.9%.
No-trap canaries flat (noop_colon +0.3~0.8% median, mins equal).
21-shape semantic battery (normal/missing/file-not-dir/bare-cd/../
trailing-slash/semicolon/pwd -P| -L/rejected shapes/side-effect-once/
status propagation/xtrace disqualification/nested/quoted/exotic):
stdout+stderr+rc byte-identical to master except the intended
once-only target expansion.
Attribution (temporary phase timers over the release build, removed
before landing): run_debug_trap re-tokenized and re-parsed the action
text on EVERY firing — measured 12.5us x 6002 firings (75ms of 4.0s)
in the bats_debug_trap shape (trap body = function call + cd/pwd comsub
+ two special-array copies). GNU parses a trap action once at trap
invocation (trap.c read_trap_action) and runs the cached parse per
firing.

Mechanics: key = the alias-spliced action text (the exact bytes
tokenize sees), value = the parsed AST in a RefCell on the Executor.
Per firing the AST is TAKEN out of the cell (the existing per-firing
command.line stamp walk mutates it), executed, and put back — a stable
action text pays zero clones and zero parses; any change to the spliced
text (trap re-set, alias-table change feeding the splice, empty->armed
transition) misses and re-parses. The early-return guards (already
running, host-internal depth) sit before the cache and never touch it,
so re-entrancy cannot double-take. The RETURN trap keeps its own path.

Construction-only: a same-text re-parse is deterministic (plain
tokenize+parse, no executor state input), and the stamp walk
unconditionally overwrites every top-level command line per firing, so
a reused AST is re-stamped exactly as a fresh parse would be. Aliases
expand at execution from the live table (parse.y alias_expand_token
ports) — unchanged.

A/B (interleaved rounds, same host, release builds, medians/mins):
bats_debug_trap shape -28.8%/-29.3% (9 rounds); with the first cut of
this series the same lane reads -32.6%/-28.2% (7 rounds). No-trap and
array-copy canaries flat (mins equal, medians within noise).

Verification: 21-shape DEBUG battery (LINENO/BASH_COMMAND, loop and
function firings, trap re-set inside action, alias text evolution after
the trap is armed, functrace inheritance, ERR/RETURN adjacency, same-
text re-arm, nested cd/pwd comsub inside the action, exit-2 terminal
skip) byte-identical to master; full cargo test --no-fail-fast failure
sets bidirectionally identical to fresh origin/master (4432 passed /
55 failed / 170 suites on BOTH sides, per-suite counts identical).
@caomengxuan666
caomengxuan666 merged commit 65caf55 into master Oct 12, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the perf/dispatch-floor 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

Development

Successfully merging this pull request may close these issues.

2 participants