Repository navigation
Description
Parent: #619 (tracking parent for the control-flow eval family; this issue splits out shape 1 of 4).
Shared floor: #607 (per-command dispatch machinery — sequence #607 first where applicable; see below).
Problem statement (measured)
Battery-1 (29 workloads, Android, rubash master 334afab vs bash 5.3.20, idle best-of-3) reported
[[ $x == 1 ]] ×200k → 3 865 ms vs 736 ms (5.3×) and case $x in a*) ×200k → 3 700 ms vs 663 ms (5.6×).
Isolation (for((;;)) form, 200k iters, : baseline 929 ms rub / 482 ms bash) proved the condition
type is irrelevant (:, true, ((1)), [[ ]] all identical) — the wrapper, not the leaf, dominated:
| construct | rub before | bash | rub Δ/op |
|---|---|---|---|
if :; then :; fi |
3104 ms | 736 ms | +10.9µs (bash 1.3) |
case $x in abc) literal |
2764 ms | 658 ms | +9.2µs (bash 0.9) |
case $x in a*|b*) glob |
3670 ms | 642 ms | +13.7µs (bash 0.8) |
[[ $x == 1 ]] standalone leaf |
1042 ms | 532 ms | +0.6µs — leaf pre-parsed, never the cost |
PR #624 (merged) removed the clone half: execute_if_command_with_redirects
(compound_exec.rs:1283) and execute_case_command_with_redirects (:2447) deep-cloned the
40-field +6/+4µs per-exec compound-dispatch class**CommandNode plus the whole If/Case subtree and ran no-op
materialize_compound_output_process_substitutions + apply_*_redirects walks per statement.
Post-#624 residual (same method): if +10.9 → +5.2µs/op (f_brack 5.2× → 2.8×),
case-lit +9.2 → +3.7µs/op (f_caseglob 5.7× → 2.7×), case-glob → +4.3µs/op.
The split-plan recon bins this residual as the **
(rubash-side dispatch delta vs bash's ~1µs); the ~1µs difference vs the table above is
Re-measured at filing on current master (23badf8, release, min-of-3 rotated): if :; then :; fi x50k 0.52s vs 0.19s (2.7x); case $x in abc) x50k 0.48s vs 0.18s (2.6x). Residual confirmed, filing as specified.
What remains is the condition/body dispatch itself, common to every compound statement.
GNU owner (vendored tree third_party/bash/)
execute_cmd.c:execute_if_command/execute_case_commandwalk the stored tree by pointer
and apply redirections only when present (redir.c do_redirectionsin the parent context).execute_cmd.c:3653: case fast path preservesline_number = case_command->line
(rubash keeps thewith_ambient_line(cmd.line)binding — see perf(1.4.1): if/case no-redirect pointer-walk fast path (if -53%, case -47%) #624).test.c/cond.c:[[ ]]leaf evaluation (conditional.rs:54walks the pre-parsed
ConditionalCommand.args, built once atparser/nodes.rs:838-864— no re-tokenization;
the earlier "re-tokenize per exec" claim in [perf][P2] Control-flow eval overhead: [[ ]] 5.3×, case 5.6×, mapfile 5.4×, glob 3.5×, source 2.8× — conditional constructs re-tokenize per exec (29-workload battery) #619 was investigated and retracted on the record).execute_cmd.c:3862 execute_if_command: walks the storedIF_COMtree by pointer
(execute_command (if_command->test)/ branch,line_numbersave/restore) — no rebuild:
save_line_number = line_number;
if_command->test->flags |= CMD_IGNORE_RETURN;
return_value = execute_command (if_command->test);
line_number = save_line_number;execute_cmd.c:3644 execute_case_command: singleexpand_word_leave_quoted
of the subject word, then per-patternexpand_word_leave_quoted+ match in the
clause loop (:3686,:3712) — redirections applied only when present.
Fix sketch (ranked; #607-machinery for this residual)
- Sequence [perf][P1] Arithmetic-for / loop execution 2.37x slower than GNU bash: per-eval state ticket, lvalue+read+write allocations, CommandNode rebuild, per-command probes #607 first. The residual lives in
execute_ast_inner+ errexit/condition-state
toggles +if_command_needs_alias_scanper-exec shopt lookup — i.e. the per-command
ticket /CommandNodeprep (command_prepare.rs) / per-command probe (ast_exec.rs)
machinery catalogued in [perf][P1] Arithmetic-for / loop execution 2.37x slower than GNU bash: per-eval state ticket, lvalue+read+write allocations, CommandNode rebuild, per-command probes #607 (and scalar memo in [perf][P1] Scalar assignment path 2.72x slower: unconditional xtrace-only memo insert, dual BTreeMap+HashMap stores with replay log, nameref pre-allocation #609). Fixing [perf][P1] Arithmetic-for / loop execution 2.37x slower than GNU bash: per-eval state ticket, lvalue+read+write allocations, CommandNode rebuild, per-command probes #607's floor first
shrinks this residual for free; measure before/after with the f_brack/f_caseglob isolations. - Then, only if the residual persists: hoist the per-exec condition-state save/restore out of
the hot path for side-effect-free conditions, and cache the alias-scan/shopt verdict per
stable node (samecommand_prepare.rsslope as [perf][P1] Arithmetic-for / loop execution 2.37x slower than GNU bash: per-eval state ticket, lvalue+read+write allocations, CommandNode rebuild, per-command probes #607 — one source of truth, no ad-hoc cache). - Explicitly out of scope here: literal-pattern work belongs to split (2/4);
[ ]builtin
(~+10µs/op,execute_test_wordsdispatch) belongs to [perf][P1] Scalar assignment path 2.72x slower: unconditional xtrace-only memo insert, dual BTreeMap+HashMap stores with replay log, nameref pre-allocation #609.
Blast radius
compound_exec.rs if/case dispatch + ast_exec.rs inner dispatch + condition-state handling.
Touches every if/case execution —ćen behavioral surface is large but the change is
mechanical (remove per-exec work, no semantic reinterpretation). Heredoc/redirect/procsub
statements keep the slow path (per #624's compound_command_has_redirects() guard).
NON-goals
- Do NOT re-architect
[[ ]]leaf evaluation (standalone leaf is +0.6µs — already cheap). - Do NOT implement generic "compile pattern set once" here — dynamic case patterns re-expand
per GNU'sexecute_cmd.c:3644loop semantics; static-literal fast path is split (2/4). - Do NOT touch
[ ]/testbuiltin dispatch (owned by [perf][P1] Scalar assignment path 2.72x slower: unconditional xtrace-only memo insert, dual BTreeMap+HashMap stores with replay log, nameref pre-allocation #609). - Do NOT regress the perf(1.4.1): if/case no-redirect pointer-walk fast path (if -53%, case -47%) #624 no-redirect fast path or its redirect/heredoc/procsub slow path.
Verification bar (gates)
- Isolation battery:
if :; then :; fi,if [[ $x == 1 ]]; then :; fi,case-lit,case-glob
(200kfor((;;)), idle best-of-3, vs bash 5.3.20) — report before/after ms + Δ/op + ratio. cargo test --lib578/578 green.cargo test --test cli_tests: 24 failed = exact [test][platform:linux] 24 cli_tests red on Android unmasked by #613 (fail-fast hid them behind the lib failures): 13 missing bashdb fixture, 6 Windows-hardcoded expectations, 3 host-tool assumptions, 2 to triage #615 ledger set, name-for-name, no new reds.cargo test --test executor_tests: failure set byte-identical to clean master
(1229 pre-existing env reds; diff sorted failure names).- Differential
probes.shbattery: 95/116 unchanged. cargo fmtclean.
Activity
docs: performance ledger points #619 at split sub-issues #661-664
caomengxuan666 commentedon Oct 16, 2026
Cut up as PR #673 (branch
perf/issue661-ifcase): empty-table alias-scan skip + unmarked bare:/trueconditions with a shadowing gate (review-caughttrue()+errexit divergence, now a regression test). 50-cmd else body -26%; guards 55/55 + 29/30. Gate running.perf(executor): if-condition dispatch residual (#661) (#673)
caomengxuan666 commentedon Oct 16, 2026
Landed as #673 (squash-merged e21b6d0). 50-cmd else body -26%; guards 55/55 + 29/30; gates identical (niu 50/49 = known load flake, green in isolation).
docs: changelog + perf ledger land #661/#662/#663/#664/#609s1/#607ticket
style: cargo fmt the #661 shadow regression tests
Merge pull request #2155 from unixwin/fix/660-661-history-write