Skip to content

WIP: perf: bind compound redirects once at entry instead of clone+splice per leaf - #2268

Merged
caomengxuan666 merged 7 commits into
masterfrom
fix/2246-redirect-clone
Oct 30, 2026
Merged

caomengxuan666 merged 7 commits into
masterfrom
fix/2246-redirect-clone

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

WIP — cluster delivery under the compile-rationing regime: code + pins + cargo check locally; 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:

  1. cmd.clone() (deep, whole node) feeding procsub materialization,
  2. a second deep clone of the body subtree, and
  3. apply_command_output_redirects splicing 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_redirects binds 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_redirects performs 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; brace finish_brace_redirect_failure with the #826 established-err-target routing preserved in prepare_brace_group_redirects; case keeps its own arm admissions in prepare_case_command_redirects).

All seven wrappers (if / loop / for / select / case / subshell / brace group) now execute the original tree. 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). 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_redirects remains 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>&1 shared description, nested shielding/inheritance, body-cd anchor, pipeline last stage inside redirected subshell/brace (#532 shape), >>/dev/stdout leaf alias, heredoc/herestring on compounds, >(...) target, restricted refusal (#804), failure status + ! non-flip (#846), create ordering + diagnostic routing (#940), function-definition non-capture, exec in subshell body, for/while/case/select coverage, append form, empty-body create, noclobber refusal, numbered fd, external leaf, the issue's measured if…fi >/dev/null shape.

Verification debt (main-session batch)

  • Done here: cargo check clean (0 warnings), cargo check --tests clean, per-file rustfmt, LF clean.
  • Owed: the 30 pins; 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 − plain if ≈ one entry open+restore (vs the : >/dev/null control), replacing the +30µs-class delta measured on master.

caomengxuan666 and others added 7 commits October 30, 2026 17:30
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.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 18:43
@caomengxuan666
caomengxuan666 merged commit 8545f2b into master Oct 30, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2246-redirect-clone branch November 2, 2026 01:08
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