Skip to content

WIP: perf(F5): declare mark point queries + getopts window borrow - #1914

Merged
caomengxuan666 merged 2 commits into
masterfrom
perf/f5-declare-getopts
Oct 28, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
perf/f5-declare-getopts

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

F5 family: declare / getopts per-call constants (perf round 2026-10-28, §5 F5)

Two mechanisms, both "cost must scale with the declaration's actual work, not table size / fixed pipeline":

1. declare/typeset/local — full-table mark renders per membership question.
Every call materialized whole-table name sets per attribute key (marked_names: iterate the attrs map, clone matching names, sort) and built HashSets from them, only to answer contains() for its own operands. A plain declare x=42 paid ~18 renders. Measured mechanism proof (same machine, Linux): 85.6 µs/op ambient env → 345.2 µs/op with +300 exported variables. All membership consumers in the declare family now use VarTable::is_marked (one attr_of lookup over the same flag bits). Listing paths that genuinely enumerate (declaration_names_to_print) are unchanged.

2. getopts — positional window cloned per call.
execute_getopts cloned the entire positional window per call when no explicit args were given: 459 µs/call scanning 3000 positionals vs 50 µs/call at 300. All reads complete before the binding calls, so the borrowed positional_window() slice now serves them.

Behavior: zero change

  • declare/getopts shape corpora (attrs, -i arith, case attrs, array/assoc conversion + errors, nameref chains + errors, readonly, export/trace, function scope, subscript hints, PATH hash-flush, BASH_COMPAT hook; getopts silent/verbose, missing/unknown, attached/grouped args, --, OPTERR=0, OPTIND mid-stream, name=OPTARG): byte-identical to GNU bash 5.3.20, stdout+stderr, before and after.
  • Full declare -p listing corpus (full + filtered listings, export -p/readonly -p, tempenv-prefixed listings, PIPESTATUS view, function scope, typeset): byte-identical old-vs-new (modulo the binary's own BASH/THIS_SH path lines).

Measured (paired interleaved A/B, same machine, Linux, release)

case before after
declare x=42 ×5000 57.97 µs/op 19.21 µs/op
same, +300 exported vars 207.13 µs/op 22.69 µs/op (table term gone)
declare -p x ×1000 159.89 52.61
bare declare x ×5000 84.83 24.37
getopts, 3000-positional scan 301.83 µs/call 22.42 µs/call

Not in this PR (recorded in the cluster REPORT for homing)

The bench-shape getopts_parse constant is dominated by the OPTIND=1 assignment statement (~+60–110 µs vs ~+0–20 for a plain bind): names excluded from bind_marks_free_scalar (OPTIND, integer-attributed, exported) pay the full apply_shell_assignment_inner path. That is the general assignment machinery — a separate family, not touched here.

…ll-table mark renders

Every declare/typeset/local call materialized whole-table name sets
per attribute key (VarTable::marked_names: iterate the attrs map,
clone matching names, sort) and built HashSets from them, just to
answer contains() for the command's own operands. A plain
'declare x=42' paid ~18 such renders; the cost scaled with the
number of attributed variables in the shell (measured: 85.6us/op
with the ambient environment, 345.2us/op with 300 extra exported
variables, same-machine Linux), which is the F5 constant the
2026-10-28 perf round recorded (declare_assign 15.77x,
declare_local_like 24.32x vs GNU 5.3.20).

All membership consumers in the declare family now use
VarTable::is_marked (one attr_of lookup; the same flag bits
marked_names renders), including:
- declare.rs: sync_typed_assignments / sync_typed_attributes
  operand checks, the nameref chain walk, the main operand loops
- assign.rs: readonly gates, the per-element array check
- attrs.rs: nameref target resolution, the visible-nameref skip
  filter, the unset-attribute branch
- print.rs: declaration_line (state-view snapshots rendered ten
  sets per name), print_declare_names (the tempenv underlay is now
  consulted per name at print time - the same answers the
  snapshot-adjusted sets gave; listing-only candidate sets in
  declaration_names_to_print are unchanged, a listing walks the
  table anyway)

Behavior is unchanged by construction (same underlying attribute
map, point query vs render of the same set) and verified:
declare/getopts shape corpora byte-identical to GNU bash 5.3.20,
full declare -p listing corpus byte-identical old-vs-new
(modulo the binary's own BASH/THIS_SH path), diffcorpus and the
lib baseline unchanged.

Measured (paired interleaved A/B, same machine, Linux):
declare_assign 57.97 -> 19.21 us/op; with +300 exported variables
207.13 -> 22.69 us/op (table-size term gone); declare -p x
159.89 -> 52.61 us/op; bare declare 84.83 -> 24.37 us/op.
execute_getopts cloned the whole positional window
(positional_window_cloned) on every call when no explicit args
were given, making each call O($#): measured 459us/call scanning
3000 positionals vs 50us/call at 300 (same machine, Linux).
The scan only reads the window - the current word is consumed
into option_chars before any binding call, the separate-argument
value is cloned at its read point, and args.len() is by-value -
so the borrowed positional_window() slice serves every read.
3000-positional scan: 301.83 -> 22.42 us/op paired A/B.
@caomengxuan666 caomengxuan666 added bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger area:expansion word splitting, quote removal, parameter/brace expansion labels Oct 28, 2026
@caomengxuan666 caomengxuan666 self-assigned this Oct 28, 2026
@caomengxuan666 caomengxuan666 removed bug Something isn't working gnu-baseline Tracked by the GNU Bash 5.3.0 true-baseline ledger area:expansion word splitting, quote removal, parameter/brace expansion labels Oct 28, 2026
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 28, 2026 05:35
@caomengxuan666
caomengxuan666 merged commit d4602b0 into master Oct 28, 2026
13 of 14 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 29, 2026
…#1951)

Re-measurement verdict for the #1951 constant tax (full data in the
cluster report): the ~0.29ms/line endpoint-specific tax measured at
the audit freeze tip is gone on current master -- the streamed
endpoint landed by rubash#1952 plus the F5 work (PR #1914) replaced
the materialized endpoint it lived on. Same-window decomposition
(Linux, same machine) now shows the while-read face tracking the
engine's general per-command floor: the endpoint drains at external
cat speed, and the shell-side read premium is source-independent
(regular file, pipe and procsub within noise of each other). GNU's
own pipe reads are byte-wise by design (read.def:668 sets
unbuffered_read for pipe input; zreadc buffering covers the file
face, where rubash already buffers), so no endpoint mechanism fix
remains that would not either duplicate the floor-tax work tracked
by rubash#682 / rubash#2005 or diverge from GNU.

These two pins guard the re-measured face in CI: exact line count
at 20k lines and whole, ordered records off the streamed endpoint.
@caomengxuan666
caomengxuan666 deleted the perf/f5-declare-getopts branch October 30, 2026 01:42
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