Skip to content

[perf][P2] Split #619 (2/4): case per-arm pattern re-expansion constant (~1.27 vs 0.51µs/arm) #662

Closed

Description

@caomengxuan666

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.c matching 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/3712 inside execute_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)

  1. Static-literal fast path in expand_case_pattern: if raw_text == text and 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).
  2. 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).
  3. 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

Verification bar (gates)

Activity

  1. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    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.

  2. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

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

  3. added
    performance
    P2-medium
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2-mediumperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions