Repository navigation
perf(1.4.1): if/case no-redirect pointer-walk fast path (if -53%, case -47%) - #624
Conversation
…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.
78667e2 to
5513e15
Compare
|
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.
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. |
Summary
ifandcasestatements paid a fixed ~11 µs / ~9 µs per execution regardless of condition complexity, because the*_with_redirectswrappers always ran a three-stage machine even when the statement carries no redirections at all:cmd.clone()— a deep clone of the ~40-fieldCommandNode,IfCommand/CaseCommandnode tree (condition + branch bodies + clause patterns — each aCommandNodewith dozens ofVecfields, eachStringheap-allocated),materialize_compound_output_process_substitutions+apply_*_redirectswalks that are guaranteed no-ops without redirect fields.GNU
execute_cmd.cdoes none of this:execute_if_command/execute_case_commandwalk the stored tree by pointer and apply redirections only when the statement has them (redir.c do_redirectionsin 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:for((;;))form, min-of-3):baseline[[ $x == 1 ]]standaloneif :; then :; fiif ((1)); then :; ficase $x in abc) …case $x in a*|b*) …ConditionalCommand.argsandCaseCommand.clausesare 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 theif/casewrappers (condition type is irrelevant::,true,((1)),[[ ]]all cost the same).Change
compound_command_has_redirects(cmd)covering every field consulted bymaterialize_compound_output_process_substitutions,apply_*_redirects, andwith_command_input_redirects(redirect_in/out/append/err/err_append,redirects, heredoc/here_string + bodies/carriers, structuredprocess_substitutions).execute_if_command_with_redirects: when false, callexecute_if_command(if_command)directly — no clones, no no-op walks.execute_case_command_with_redirects: same fast path, keeping thewith_ambient_line(cmd.line)binding (GNUexecute_cmd.c:3653line_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:
if :; then :; fiif [[ $x == 1 ]]; then :; ficase $x in abc)case $x in a*)(glob)[[ ]]standaloneRegression gates:
cargo test --lib→ 578/578 greencargo test --test cli_tests→ 24 failed = the 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 set (family A 13 bashdb + B 6 Windows + C 3 host-tool + D 2 triage — name-for-name identical, no new reds)cargo test --test executor_tests→ failure set byte-identical to clean master (1229 pre-existing environment reds, present on both checkouts; see the note on [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 — this binary is a pre-existing stratum, not touched by this PR; diff of sorted failure names shows only one line-mangling artifact)probes.shbattery → 95/116 unchanged (no user-visible compat regression)cargo fmtcleanResiduals (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_patternstring churn for literal patterns), and the[ ]builtin path (+10 µs/op, visible inf_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.