Skip to content

perf(for-arith): counter fast driver for canonical counting loops - #925

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/loop-iter-overhead
Oct 20, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/loop-iter-overhead

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Whitelist fast driver for for ((v=C0; v<C1; v++)) with bare : body (entry gates: slow-init ran, marks clean, empty write log, no traps, xtrace/verbose off, : not disabled, no jobtable/background children). Reuses parse-cache admission + PostInc write guards; i64 wrapping count, same observable state per round (i/_/exit/line keys), signal mailbox drained per 1024 rounds. GNU: execute_cmd arith-for + expr.c. Tests: lib 645 + cli 467 green; 46 bash diffs pass (3 diverges pre-existing, gated). Release N=200k empty body: 1038.7ms -> 131.3ms (0.21x bash).

… :; done`

GNU owns the semantics: execute_arith_for_command (execute_cmd.c:3219)
re-evaluates test/update via expr.c descent and dispatches the body via
execute_command_internal per iteration, with line_number pinned to the for
line (execute_cmd.c:3236) and DEBUG/REAP handling per iteration. For the
canonical counter triple over one plain variable with a single bare-`:`
body, every per-iteration effect is idempotent or provably unobservable
mid-loop (no DEBUG action, trace, trap, job or failure can appear; the
`:` body cannot arm any of them), so the driver performs the identical
counter directly (i64 wrapping per bash_arith), the identical PostInc
write pipeline (same record/sync calls, same order) and the identical net
end-state (line/command stamps via the same helpers, `_`=`:` , exit 0).

Whitelist admission only (rubash#117): counter shapes reuse the
parse-cache gates, the body check mirrors #661 field-for-field, and any
doubt (other operators/updates/bodies, non-canonical bounds, marked or
dynamic loop variable, pending write log, installed trap action, xtrace,
verbose, disabled/shadowed `:`, background jobs) runs the slow loop
unchanged. Verified: 46-case bash differential battery (43 identical,
3 pre-existing GNU divergences byte-identical before/after), lib 645/0,
cli 467/0. N=200k release best-of-3: empty loop 1038.7ms -> 131.3ms
(0.21x bash); add loop unchanged (1.31x bash, slow path by design).
@caomengxuan666
caomengxuan666 merged commit 960664a into master Oct 20, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/loop-iter-overhead branch October 21, 2026 08:03
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