Skip to content

perf(test): fast-expand [ $VAR -op N ] integer comparisons - #990

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/testvar-int
Oct 22, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/testvar-int

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Variable-operand twin of the literal-words fast path: [ $i -lt N ] paid the full per-word expansion pipeline per iteration for a single scalar read. New whitelist try_expand_test_var_int_command resolves plain $NAME operands directly (same dynamic->shell->env chain as walker unquoted arm) when the value is canonical decimal (reuses builtins::test::fast_decimal, no second copy) and IFS-free. All doubt shapes fall through (quotes, positionals, unset/empty/non-canonical/splittable, arrays/namerefs, string ops, arity). Tests: lib 689 + new admission/construction unit test + coherence fuzz green; 35-case GNU differential identical. Loop-while 30k: ~2.4x->~1.8x vs bash.

GNU test.c:357 arithcomp (via valid_number, general.c:248-281) is the
spec; the decimal fast path (fast_decimal) already covered literal
operands, but every `$VAR` word missed the multi-word literal whitelist
and paid the full per-word expansion pipeline (pre-scans, brace and
positional probes, field-split checks, materialize).

gdb sampling, 30x on `while [ $i -lt 100M ]` (release): 9/30 in
expand_command_words/expand_command_word, 0 in execute_test_words,
valid_number, fast_decimal or fast_arith -- the comparison was never
the hotspot; expansion was.

New whitelist in expand_command_words (rubash#117 discipline -- doubt
falls through to the real pipeline): `[` (5 words) / `test` (4 words)
with one of the six integer operators, no expandable structure (shared
predicate), raw == word everywhere, literal operands via the shared
fast-literal predicate, and `$NAME` operands resolved through the
walker's unquoted `$name` chain (dynamic_parameter_value ->
shell_variable_value -> exact_case_env_var) gated on scalar (arrays,
assocs and namerefs keep the slow path), canonical fast_decimal value
(reused, never duplicated: `+12`/`007`/whitespace/empty/overflow keep
the slow path with exact GNU diagnostics) and no byte in the current
IFS (unquoted splitting provably identity). Quoted `"${..}"`,
positionals, unset and splittable values all decline. Construction
mirrors try_expand_literal_words_command field-for-field.

release best-of-3, Termux aarch64 (parallel builds contending, so read
the within-run delta): var-iso `[ $i -lt 15000 ]` vs literal-iso delta
152ms -> 70ms per 30k (5.1us -> 2.3us per operand); after-fix sampling
3/30 in expand (resolver + shared preserves_word_metadata scan), rest
is the shared dispatch floor (bash_command_text, set_current_command)
also paid by literals. Quoted `"[ $i ... ]"` unchanged (slow path).
@caomengxuan666
caomengxuan666 merged commit e016765 into master Oct 22, 2026
8 checks passed
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/testvar-int branch October 22, 2026 03:26
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