Skip to content

perf(builtin): short-circuit silent builtins with empty output and no redirects - #984

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/silent-builtin-output-shortcircuit
Oct 22, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/silent-builtin-output-shortcircuit

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Silent builtins (shift etc.) ran the full write_buffered pipeline per command: marker-decode allocs, fd-table HashMap seeding, ordered walk, zero-byte writes. When stdout/stderr buffers are empty AND no redirects/heredocs are present, skip straight to Ok (clearing last_builtin_write_failed). Any redirect or non-empty buffer takes the original path; fd-closed diagnostics only fire on non-empty output (verified: silent shift vs echo hi with closed stdout). Honest result: shift ~1.5-4% (still ~2x bash — its bottleneck is elsewhere), export/unset unchanged (their cost is operand rewrite + typed sync). Zero semantic drift, verified byte-identical vs bash.

… redirects

shift and friends succeed with empty stdout/stderr buffers on the hot
path; skip the full write_buffered pipeline (marker decode alloc,
fd-table Hash seeding, ordered redirect walk, 0-byte writes) when
cmd carries no redirects and both buffers are empty. Any redirect or
non-empty buffer takes the slow path; the fd-closed diagnostic only
fires on non-empty output, so skipping is observably identical.
@caomengxuan666
caomengxuan666 merged commit 6a38de0 into master Oct 22, 2026
8 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 22, 2026
…eave loop (#984/#986/#920 follow-up)

The export/unset 2k interleave sat at ~2x vs GNU bash after the
short-circuit (#984), IFS fast lane (#986) and single-probe (#920)
passes. Release best-of-7 curves on this host (Termux aarch64):

  interleave 2k/4k/8k: 1.88x / 2.18x / 2.25x (was 1.9x / 2.7x / 2.9x)
  export-only  2k/4k/8k: 1.64x / 1.74x / 1.98x (was 1.45x / 2.3x / 2.4x)
  unset-only   2k/4k/8k: 1.71x / 2.00x / 2.02x (was 2.2x / 2.4x / 2.4x)
  same-machine A/B vs pristine: -16% (2k) / -22% (4k) / -24% (8k)

30-sample gdb profile of the loop body (release) pointed at the same
shape everywhere: no single hot function, but ~15 redundant per-word
pre-scans plus 2-4 small allocs per word in the expand tail, the
BASH_COMMAND re-render, the pathname probe, and the declare rewrite.
Each cut below is a whitelist/dedup with a provable identity (any
doubt falls through to the slow path, rubash#117):

- pathname_expand_word: byte gate (no `*?[]()+@!` -> NoMatch), skips
  seven option lookups + the quote-aware scan per plain word.
- set_current_command: raw-join fast render (raw == word for every word,
  no `$(`/backtick, no assignments/redirects) -> words.join(" "),
  pinned fast == slow by unit test.
- scan_substitution_spans: early empty-Vec without `$(`/backtick
  (only two push sites, both need a trigger byte).
- raw_word_contains_process_substitution (x2 sites) +
  raw_word_has_unquoted_process_substitution: early false without
  `<(`/`>(`.
- expand_simple_substitution_fragments call site: skip without
  `$(`/backtick (splitter provably returns None).
- strip_ifs_protection_markers + decode_command_substitution_payload:
  Cow forms on the hot tail (borrow when unmarked).
- splits_unquoted_expanded_word: default-IFS no-split pre-check
  (no IFS whitespace/marker bytes -> false, skips guard-convert
  alloc + split materialization).
- rewrite_declare_operand_subscripts: whole-list fast path for plain
  operands (no dash args, no `[`, no `(`, no compound marker).
- export mirror loop: borrow instead of clone; unset names/variable_args
  borrowed; unset_name borrow; raw_word_is_fully_single_quoted first-byte
  gate.

Probes byte-identical to bash and to the pristine binary (glob,
custom-IFS splits, ANSI-C guards, nullglob/failglob, declare -a/-A/
-i/subscript/append, unset -v/-f/readonly/function, BASH_COMMAND,
process substitution, backtick/nested $()).
lib 689 green; executor_tests failure set identical to pristine
(13 pre-existing Termux-env failures); cli_tests 10 failures are
missing-submodule fixtures + dirty-tree version hash (both pre-date
this change and clear on a clean tree).

Next Top (post-cut 15-sample): allocator frames dominate (scudo
~60% of innermost hits) — per-word String/Vec churn remains the
frontier; bind_underscore / tempenv restore are at GNU parity.
caomengxuan666 added a commit that referenced this pull request Oct 22, 2026
docs(readme): ledger release qualifier + perf #984/#985/#986/#987/#990
@caomengxuan666
caomengxuan666 deleted the fix/silent-builtin-output-shortcircuit branch October 22, 2026 03:26
caomengxuan666 added a commit that referenced this pull request Oct 22, 2026
…eave loop (#984/#986/#920 follow-up)

13 files, all whitelist/alloc-elision, doubt falls through: no-pattern
gates for glob/procsub/substitution scans, Cow for IFS/payload decodes,
raw-join BASH_COMMAND render with fast==slow contract test, export mirror
borrow, unset borrowed names/flags, first-byte gate for single-quote
tiling, plain-operand declare rewrite. Interleave 2k: 60ms->51ms vs bash
27-30ms (~2.2x->~1.9x, ~15%).

Note: the splits_unquoted_expanded_word no-IFS short-circuit was reverted
before merge — Windows-only upstream-suite (exp) snapshot divergence, root
cause not yet isolated; Linux runs were byte-identical. Tracked separately.
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