Repository navigation
WIP: perf: bind compound redirects once at entry instead of clone+splice per leaf - #2268
Merged
Merged
Conversation
Refs #2246 A redirected compound command paid, per execution: a deep clone of the whole command node, a second deep clone of the body subtree, and a splice walk injecting an APPEND-mode copy of each stdio redirect into every leaf (each leaf then re-opened the target). GNU runs do_redirections ONCE at compound entry (execute_cmd.c:854-883, dispose :1762; redir.c:767-955) and the body inherits the bound fd table. The entry-binding half of that model already existed here: with_command_input_redirects binds every redirect from the ordered list once (independent OFDs per #1068, noclobber prepared-target consumption, fd-0 staging), and every consumer of the spliced entries already treated them as "realized by the fd-table binding - skip". What the splice arms additionally performed, and what this change preserves verbatim, is the parse-order entry side effects: target expansion in parse order (incl. the failglob abort, subst.c:12663-12668) and the one create/truncate per file target (#940 ordering, #846 failure adjudication at the wrappers). - New prepare_compound_output_redirects (plus case/brace variants keeping their own arm admissions and, for brace, the #826 established-err-target report routing) performs only that entry half; all seven compound wrappers (if/loop/for/select/case/ subshell/brace group) now execute the original tree against the entry binding. - The cmd clone survives only behind compound_redirect_needs_procsub_rewrite (a mirror target starting with ">(" - the #117 whitelist gate for the one remaining mutation, procsub materialization). - The subshell's stripped stdio node (for its EXIT trap) is built lazily behind the trap runner's own early-out verdicts instead of deep-cloning the command node for every subshell execution. - apply_command_output_redirects remains for trap bodies, fc, source, script files, and the alias-stream loop path, whose bodies are not compound entries. 27 GNU-semantics pins in tests/issue2246_compound_redirect_entry_binding_regressions.rs (shared/independent offsets, dup order, nested shielding, entry-cwd anchor, pipeline stages, fd-alias targets, heredoc/herestring, procsub target, restricted refusal, not-flip, create ordering, noclobber). Local verification per the compile-rationing regime: cargo check + cargo check --tests clean; test runs and the release benchmark ride the main session's batch.
Refs #2246 The procsub_target_on_if pin used `fi >(cat > cap.txt)`: a bare `>(...)` after a compound command is a process-substitution WORD, not a redirection operator, and GNU bash 5.3.20 rejects the shape with a syntax error (rc=2) — the pin's silent-success expectation could never hold under GNU semantics. The legal spelling of the intended shape is a redirect TO a process substitution: `fi > >(cat > cap.txt)`. Pin assertions (cap=[hi], empty stderr, rc 0) unchanged; doc comment now records the spelling rule. Oracle ledger (GNU 5.3.20, exact pin script bytes, fresh scratch dir per run): original shape rc=2 syntax error; corrected shape rc=0 with cap=[hi] as the modal outcome (8/10 — GNU does not wait for the procsub consumer before the next command reads the file, so the immediate read is racy under GNU itself). Rubash's materialization of a compound-redirect procsub target drains before the compound returns: branch binary 20/20 cap=[hi], and the full pin target passes 30/30 across 4 consecutive runs. The other 29 pin shapes were re-scanned against the oracle in the same pass: every shape is valid under GNU and every stdout/exit expectation matches the pin byte-for-byte (diagnostic prefix rendering aside — that is rubash's program-name convention, not a shape property). No product code changed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WIP — cluster delivery under the compile-rationing regime: code + pins +
cargo checklocally; test runs and the release benchmark are the main session's batch (see "Verification debt"). Do not merge from CI alone.Refs #2246
What
Compound-command redirections now bind once at entry (GNU execute_cmd.c:854-883 do_redirections, dispose :1762; redir.c:767-955) instead of the old per-execution cost stack:
cmd.clone()(deep, whole node) feeding procsub materialization,apply_command_output_redirectssplicing an APPEND-mode copy of each stdio redirect into every leaf, each leaf re-opening the target at run time.The entry-binding half of GNU's model already existed and already ran on every compound:
with_command_input_redirectsbinds the ordered redirect list once (one open per redirect, independent OFDs per #1068, noclobber prepared-target consumption, dev-stdio dup resolution, fd-0 staging). Every consumer of the spliced entries already treated them as "realized by the fd-table binding — skip". So the splice was a second, redundant delivery mechanism.New
prepare_compound_output_redirectsperforms only the apply arms' non-mutating entry prefix, in parse order (#940): target expansion (incl. the failglob abort, subst.c:12663-12668) and the one create/truncate per file target with the arms' existing dup/closed/null-device carve-outs. Wrappers keep their exact error adjudication (IoError →note_compound_redirect_failure, #846; subshell state-restore, #940; bracefinish_brace_redirect_failurewith the #826 established-err-target routing preserved inprepare_brace_group_redirects; case keeps its own arm admissions inprepare_case_command_redirects).All seven wrappers (if / loop / for / select / case / subshell / brace group) now execute the original tree. The
cmdclone survives only behindcompound_redirect_needs_procsub_rewrite(a mirror target starting with>(— the #117-whitelist gate for the one remaining mutation). The subshell's EXIT-trap stdio node is built lazily behind the trap runner's own early-out verdicts instead of deep-cloning the command node for every subshell execution.apply_command_output_redirectsremains for trap bodies,fc,source, script files, and the alias-stream loop path (not compound entries).Pins
tests/issue2246_compound_redirect_entry_binding_regressions.rs— 30 GNU-semantics tests: condition+leaf coverage, shared offset across leaves, independent offsets for>f 2>f, dup ordering both ways,>f 2>&1shared description, nested shielding/inheritance, body-cdanchor, pipeline last stage inside redirected subshell/brace (#532 shape),>>/dev/stdoutleaf alias, heredoc/herestring on compounds,>(...)target, restricted refusal (#804), failure status +!non-flip (#846), create ordering + diagnostic routing (#940), function-definition non-capture,execin subshell body, for/while/case/select coverage, append form, empty-body create, noclobber refusal, numbered fd, external leaf, the issue's measuredif…fi >/dev/nullshape.Verification debt (main-session batch)
cargo checkclean (0 warnings),cargo check --testsclean, per-file rustfmt, LF clean.cargo test --test regression+ the redirect battery (issue826 / issue0113 / heredoc targets) and a lib-test comparison vs master; the release benchmark (script delivered with the cluster REPORT: same script drives branch binary + GNU oracle, min-of-5, 20k iters, Linux same-machine, release only). Acceptance:if…fi >/dev/null− plainif≈ one entry open+restore (vs the: >/dev/nullcontrol), replacing the +30µs-class delta measured on master.