Skip to content

perf(executor): zero-alloc static case-arm patterns (#662) - #669

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/issue662-case-arm
Oct 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/issue662-case-arm

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

perf(executor): zero-alloc static case-arm patterns (#662)

What

expand_case_pattern ran the full word-expansion pipeline + ~5 String
layers per clause per exec, even when the pattern is a static literal.
GNU re-expands dynamic patterns per arm (execute_cmd.c:3686/3712
expand_word_leave_quoted, match via match.c/cond.c) — a static
literal trivially expands to itself.

Fix

Default-deny whitelist classifier over both stored text and
raw_text: only inert chars admit (rejects $, backquote, quotes,
backslash, ~{<>}, controls, all PUA carrier markers — tilde probed:
HOME=/tmp/h; case /tmp/h in ~) is a HIT in GNU, so ~ falls
through). Admitted arms return Cow::Borrowed, zero alloc, pipeline
skipped; everything else takes the byte-identical slow path. No shopt
semantics moved (nocasematch was already hoisted; matcher selection
stays syntax-based).

Measured (release, order-rotated min-of-3, 20k iters, N arms)

bench n=1 n=4 n=8 n=16
literal −20% −22% −39% −39%
var −14% −24% −25% −34%
glob −7% −29% −38% −44%

Per-arm slope ~2.5µs → ~1µs (residual = matcher + loop machinery).

Verification

  • cargo test --lib 588/588; cargo fmt --check clean.
  • casearm_diff.sh 29/30 (only pre-existing Fix Winuxsh framework glob loading and hook dispatch #17, untouched slow path);
    ifcase_diff.sh 55/55 vs GNU.
  • New case_arm_static_tests (3 tests): static shapes borrow, dynamic
    shapes take the slow path with correct expansion, 9 script shapes match
    GNU exit codes.
  • Full serial failure-set gate vs master: target sets identical
    (NEW=0/DISAPPEARED=0). Test-level NEWs all exonerated: 9
    cli_tests::examples = worktree-missing-submodule artifact
    (third_party/bash empty in fresh worktrees — git submodule update --init + re-run 9/9 green); 5 part_080 coproc = known flake family
    (all pass in isolation on branch); unclosed_arithmetic_subscript
    fails identically on both legs in isolation (shared, order-dependent).
  • niubash product A/B (niubash pinned 6295367, only rubash [patch]
    switched): failure sets byte-identical (49=49).

expand_case_pattern ran the full word-expansion pipeline plus ~5
String layers per clause per exec even for static literals. GNU
re-expands dynamic patterns per arm (execute_cmd.c:3686/3712
expand_word_leave_quoted, match via match.c/cond.c) but a static
literal trivially expands to itself.

Whitelist classifier (default-deny on both text and raw_text; rejects
$, backquote, quotes, backslash, ~ { < > and all carrier/control
chars; probed GNU tilde-in-pattern HIT) returns Cow::Borrowed with no
alloc; anything else takes the byte-identical slow path. nocasematch
was already hoisted; matcher selection stays syntax-based.

Measured (release, rotated min-of-3, 20k iters): per-arm slope
~2.5us -> ~1us (n=16 literal -39%). Guards: casearm 29/30 (only
pre-existing #17), ifcase 55/55, lib 588/588.
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.

1 participant