Skip to content

perf(1.4.1): if/case no-redirect pointer-walk fast path (if -53%, case -47%) - #624

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/compound-no-redirect-fastpath
Oct 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/compound-no-redirect-fastpath

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Summary

if and case statements paid a fixed ~11 µs / ~9 µs per execution regardless of condition complexity, because the *_with_redirects wrappers always ran a three-stage machine even when the statement carries no redirections at all:

  1. cmd.clone() — a deep clone of the ~40-field CommandNode,
  2. a deep clone of the entire IfCommand/CaseCommand node tree (condition + branch bodies + clause patterns — each a CommandNode with dozens of Vec fields, each String heap-allocated),
  3. materialize_compound_output_process_substitutions + apply_*_redirects walks that are guaranteed no-ops without redirect fields.

GNU execute_cmd.c does none of this: execute_if_command / execute_case_command walk the stored tree by pointer and apply redirections only when the statement has them (redir.c do_redirections in the parent context). rubash's own code comments already cite this ("GNU walks the branch body by POINTER") — but the wrapper cloned first anyway.

Attribution correction (supersedes the earlier note on #619)

The original issue hypothesized per-exec re-tokenization of [[ ]]/case. Line-level investigation + isolation measurements show that was wrong:

construct (200k iters, for((;;)) form, min-of-3) rub before bash rub Δ per op
: baseline 929 ms 482 ms —
[[ $x == 1 ]] standalone 1042 ms 532 ms +0.6 µs/op — leaf is pre-parsed & cheap
if :; then :; fi 3104 ms 736 ms +10.9 µs/op (bash 1.3)
if ((1)); then :; fi 3020 ms 658 ms +10.5 µs/op
case $x in abc) … 2764 ms 658 ms +9.2 µs/op (bash 0.9)
case $x in a*|b*) … 3670 ms 642 ms +13.7 µs/op (bash 0.8)

ConditionalCommand.args and CaseCommand.clauses are pre-parsed at parse time — there is no re-tokenization. The measured 5.3×/5.6× battery numbers were dominated by the clone machinery inside the if/case wrappers (condition type is irrelevant: :, true, ((1)), [[ ]] all cost the same).

Change

  • Add compound_command_has_redirects(cmd) covering every field consulted by materialize_compound_output_process_substitutions, apply_*_redirects, and with_command_input_redirects (redirect_in/out/append/err/err_append, redirects, heredoc/here_string + bodies/carriers, structured process_substitutions).
  • execute_if_command_with_redirects: when false, call execute_if_command(if_command) directly — no clones, no no-op walks.
  • execute_case_command_with_redirects: same fast path, keeping the with_ambient_line(cmd.line) binding (GNU execute_cmd.c:3653 line_number = case_command->line).

Statements with redirects/heredocs/procsubs take the existing slow path unchanged.

Validation (idle best-of-3, rubash master 334afab vs GNU bash 5.3.20)

Performance:

workload before after bash ratio vs bash
if :; then :; fi 3104 ms 2047 ms (+5.2 µs/op) 732 ms 4.2× → 2.8×
if [[ $x == 1 ]]; then :; fi 3722 ms 2135 ms 756 ms 5.2× → 2.8×
case $x in abc) 2764 ms 1762 ms (+3.7 µs/op) 694 ms 4.0× → 2.5×
case $x in a*) (glob) 3670 ms 1875 ms (+4.3 µs/op) 699 ms 5.7× → 2.7×
[[ ]] standalone 1042 ms 1033 ms (unchanged) 533 ms 2.0× (was never the cost)

Regression gates:

Residuals (documented, tracked separately): the remaining ~5 µs/if is the condition/body dispatch itself (execute_ast_inner + errexit/condition-state toggles), the remaining ~3.7 µs/case is per-clause pattern re-expansion (expand_case_pattern string churn for literal patterns), and the [ ] builtin path (+10 µs/op, visible in f_test) — these belong to the per-command-machinery phase (#607/#609).

Partially addresses #619. This PR optimizes if/case dispatch only; mapfile, glob, source and remaining control-flow costs still require separate evidence and follow-up. Keep #619 open.

…nt has no redirects

if/case execution paid a fixed per-statement cost because
execute_if_command_with_redirects / execute_case_command_with_redirects
always ran: (1) cmd.clone() over the ~40-field CommandNode, (2) a deep
clone of the whole IfCommand/CaseCommand node tree, (3) materialize +
apply of redirections that are guaranteed no-ops without redirect fields,
mirroring GNU execute_cmd.c which walks the stored tree BY POINTER and
applies redirections only when present.

Add compound_command_has_redirects() covering every field consulted by
materialize_compound_output_process_substitutions / apply_*_redirects /
with_command_input_redirects, and take a pointer-walk fast path when it
is false. The case fast path keeps the with_ambient_line binding
(GNU line_number = case_command->line).

Measured (idle best-of-3, vs GNU bash 5.3.20, 200k iterations):
- if :; then :; fi      +10.9 -> +5.2 us/op (bash 1.3)
- if [[ $x == 1 ]];..  3722 -> 2135 ms (5.2x -> 2.8x)
- case literal          +9.2 -> +3.7 us/op (bash 0.9)
- case glob             3670 -> 1875 ms (5.7x -> 2.7x)
- [[ ]] standalone unchanged (+0.6 us/op; the leaf was never the cost)

Gates: lib 578/578; cli_tests 24 failed = exact #615 ledger set (A13+B6+C3+D2,
no new reds); executor_tests failure set byte-identical to master (1229
pre-existing environment reds, same on both checkouts); probes battery
95/116 unchanged; cargo fmt clean.

Closes #619.
@caomengxuan666
caomengxuan666 force-pushed the perf/compound-no-redirect-fastpath branch from 78667e2 to 5513e15 Compare October 15, 2026 07:03
@caomengxuan666
caomengxuan666 merged commit ca58ca7 into master Oct 15, 2026
6 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

Independent post-merge review completed: baseline 20268da / candidate 5513e15, native Windows release rubash.exe, rotating interleaved baseline/candidate/GNU, 2 warmups plus 9 measured rounds, 30k iterations per workload.

workload baseline median ms candidate median ms reduction
if : 708.01 524.75 25.88%
if [[ ]] 881.17 548.37 37.77%
case literal 620.29 424.24 31.61%
case glob 734.55 438.84 40.26%

All benchmark output/stderr/status matched. Semantic probes: 45/45 base=head; 43/45 also match designated WSL GNU Bash 5.3.0. Both remaining procsub differences already exist in baseline. GNU timings include WSL startup, so no same-platform GNU speed ratio is claimed. Unaffected controls showed some noise; no universal no-regression claim.

Evidence retained locally under target/issue-suites/results/pr624-review/{benchmark.json,semantics.json,review.py}. The later master including #345/#625 was not covered by this benchmark. This supports the limited if/case optimization only; mapfile/glob/source and full #619 scope remain open.

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