Skip to content

perf(shopt): don't materialize default-equal state, keep fast lookup path - #915

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/normalize-shopt-state
Oct 20, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/normalize-shopt-state

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Root cause of the niu-vs-rubash gap

Measured on-device (aarch64, best-of-3, 30k empty commands): niu 191 ms vs rubash 150-158 ms — ~1 us of host overhead per simple command, scaling to ~0.5-2x on interpreter loops (niubash#352 matrix).

Bisected with a minimal reproducer crate (engine as path dep, niubash-like thin-LTO profile — same speed as rubash, so not codegen) down to one call: hosts pinning expand_aliases off at startup. That single redundant set_option materialized $__RUBASH_SHOPT_STATE and flipped every subsequent option_enabled lookup from the allocation-free static path to the serialized split-scan — including the per-command alias gate and the per-eval/per-glob probes (#156/#157).

Fix (src/builtins/shopt.rs, +39/-1)

set_option normalizes: a state equal to the defaults removes the entry instead of storing it. All readers already treat absent as default (state_contains None arm, alias_expansion_enabled, shopt_state_enabled, histappend probe), so shopt -p/BASHOPTS rendering is byte-identical.

Verification

  • New unit test redundant_writes_normalize_to_absent_state (+ deviation round-trip).
  • 30k empty-command loop: niu 191 -> 155 ms (rubash 150-158, gap closed).
  • Full suite: cli 466/466, lib 636/636, executor 11 pre-existing Android-env failures only (one flaky wait_n under load, passes 3/3 isolated and clean on re-run).

…path

Root cause of the niu-vs-rubash gap (measured ~1us per simple command,
~0.5-2x on interpreter loops): a single redundant set_option call —
e.g. hosts pinning expand_aliases off at startup, which equals the
default — materialized $__RUBASH_SHOPT_STATE and flipped EVERY
subsequent option_enabled lookup from the allocation-free static path
to the serialized split-scan (plus per-command alias/arith/glob
probes that ride on it).

set_option now normalizes: state equal to the defaults removes the
entry instead of storing it. Reads already treat absent as default,
so shopt -p/BASHOPTS rendering is byte-identical.

Verified on-device (aarch64, best-of-3): 30k empty-command loop
niu 191ms -> 155ms vs rubash 150-158ms (gap closed, was ~25ms).
New unit test locks the normalization in.
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