Repository navigation
Description
Parent: #619 (tracking parent; this issue splits out shape 2 of 4).
Shared floor: #607 (per-command machinery — sequence #607 first; this issue stacks on top).
Problem statement (measured)
After PR #624 removed the if/case wrapper clones, the remaining case cost decomposes into the
dispatch residual (split 1/4) plus a per-arm pattern constant: prior recon reports each
non-taken case arm costs rubash ~1.27µs vs bash's ~0.51µs (ratio scales linearly with arm
count; verify arm-sweep 1/4/8/16 arms at filing — isolation scripts in
perf_battery/attrib_619/c_lit.sh, c_var.sh, f_caselit.sh, f_caseglob.sh).
Post-#624 table anchor: case-lit residual +3.7µs/op total, of which ~3µs is this per-clause
re-expansion for literal patterns (PERF_REFACTOR_PLAN Phase 3 residual note).
Root cause (code-level): every clause pattern runs the full expand_case_pattern pipeline on
every execution — full word-expansion pass over the pattern plus ~5 layers of String
allocation — even when the pattern is a static literal (raw_text == text, no $, backquote,
quote, or backslash). A stable literal abc) therefore pays expansion + allocation churn
proportional to arm count before a single comparison happens.
GNU owner (vendored tree third_party/bash/)
execute_cmd.c:3644: the case-match loop — patterns are expanded/matched per arm per
execution (match.c/cond.cmatching primitives). Dynamic patterns MUST re-expand
here (that is GNU semantics, not overhead).- Static literals are the exception worth short-circuiting: when the pattern word contains no
expansions, GNU's path reduces to comparison without the expansion pipeline. execute_cmd.c:3686/3712insideexecute_case_command: the subject word expands once,
then every pattern re-expands per execution in the clause loop — dynamic patterns
MUST re-expand here (GNU semantics, not overhead):
wlist = expand_word_leave_quoted (case_command->word, 0);
...
for (list = clauses->patterns; list; list = list->next)
{
es = expand_word_leave_quoted (list->word, 0);Fix sketch (ranked; static-literal fast path)
- Static-literal fast path in
expand_case_pattern: ifraw_text == textand the text
contains no$, backquote, quote char, or backslash → return identity (borrowed/Cow,
zero allocation, skip the whole pipeline). Ranked first: smallest diff, covers the entire
literal-arm class, semantics-preserving by construction (no expansion characters ⇒
expansion is the identity function). - Only afterwards, if glob arms still dominate: hoist per-execution shopt lookups
(nocasematch/extglob) out of the per-arm loop (read once per statement execution). - Do NOT cache across executions keyed by node: arm patterns with expansions are dynamic by
definition.
Blast radius
expand_case_pattern + case-match loop in compound_exec.rs (execute_case_command,
:2263 area). Literal-arm scripts get faster; glob/dynamic arms unchanged. Risk is
misclassifying a pattern as static (a missed $/backquote/quote/backslash = wrong match) —
the classifier predicate is the entire correctness surface; fuzz literal-vs-dynamic boundary.
NON-goals
- Do NOT implement a generic "compile pattern set once" cache keyed by source node —
dynamic patterns re-expand per execution per GNUexecute_cmd.c:3644loop semantics; a
cross-execution cache would be a semantic change (stale variable captures), not an optimization. - Do NOT touch glob-matcher internals here (owned by glob-adjacent work / split 4/4's
slice-matching only insofar as it shares the matcher). - Do NOT touch the dispatch residual (split 1/4) or the [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 floor.
Verification bar (gates)
- Arm sweep: 1/4/8/16 literal arms + 1/4/8 glob arms, per-arm Δ/op vs bash (target: literal
per-arm constant approaching bash's ~0.5µs; totalcase-litresidual down accordingly). - Dynamic-pattern parity:
$var/command-substitution/quote/backslash patterns match bash
byte-for-byte (existing case suites + targeted fuzz of the static classifier boundary). cargo test --lib578/578;cli_tests= 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 24, no new reds;
executor_testsset byte-identical to master;probes.sh95/116 unchanged;cargo fmtclean.
Activity
caomengxuan666 commentedon Oct 16, 2026
Cut up as PR #669 (branch
perf/issue662-case-arm): static-literal arms return Cow::Borrowed, zero alloc; default-deny classifier (tilde verified HIT in GNU, falls through). Per-arm slope ~2.5us->~1us; guards casearm 29/30 (only pre-existing #17), ifcase 55/55. Gate running.perf(executor): zero-alloc static case-arm patterns (#662) (#669)
caomengxuan666 commentedon Oct 16, 2026
Landed as #669 (squash-merged fddf47f). Per-arm slope ~2.5us->~1us; guards casearm 29/30 (only pre-existing #17), ifcase 55/55; gates identical (niu 49=49).
docs: changelog + perf ledger land #661/#662/#663/#664/#609s1/#607ticket
fix(executor): dequote for-list bindings so loop vars glob (#675) (#683)