Skip to content

[perf][P2] Split #619 (1/4): if/case compound-dispatch residual after #624 (~2.5–2.8×, +5.2/+3.7µs per exec) #661

Closed

Description

@caomengxuan666

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 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 **
+6/+4µs per-exec compound-dispatch class**
(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/)

  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: single expand_word_leave_quoted
    of the subject word, then per-pattern expand_word_leave_quoted + match in the
    clause loop (:3686, :3712) — redirections applied only when present.

Fix sketch (ranked; #607-machinery for this residual)

  1. 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_scan per-exec shopt lookup — i.e. the per-command
    ticket / CommandNode prep (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.
  2. 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 (same command_prepare.rs slope 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).
  3. Explicitly out of scope here: literal-pattern work belongs to split (2/4); [ ] builtin
    (~+10µs/op, execute_test_words dispatch) 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

Verification bar (gates)

Activity

  1. added a commit that references this issue on Oct 16, 2026
  2. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    Cut up as PR #673 (branch perf/issue661-ifcase): empty-table alias-scan skip + unmarked bare :/ true conditions with a shadowing gate (review-caught true()+errexit divergence, now a regression test). 50-cmd else body -26%; guards 55/55 + 29/30. Gate running.

  3. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

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

  4. added 2 commits that reference this issue on Oct 16, 2026
  5. added
    performance
    P2-medium
    on Oct 17, 2026
  6. added a commit that references this issue on Oct 30, 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