Skip to content

WIP: perf(assoc): answer settled assoc element misses from the typed cell (F3) - #1919

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/assoc-miss-settled-read
Oct 28, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/assoc-miss-settled-read

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Family F3 — associative-array element read path (perf round 2026-10-28)

WIP — gates pending; do not merge yet.

Mechanism (measured, instrumented build of master 779b0cc)

The perf round attributed the assoc miss cost to the typed-fetch gate
staying closed after loop fill. Instrumentation of this tree shows the
gate is in fact OPEN after loop fill (dirty=false, the fetch serves
loop reads from the typed cell, zero mirror fetches on hits). The
Θ(n) miss cost lives one layer up:

  • expand_braced tries expand_braced_indexed_parameter first; the
    typed fetch returns the authoritative miss (None).
  • The chain falls through to expand_braced_operator_or_array_parameter
    (src/executor/expand_braced_ops.rs), whose assoc leg re-derived the
    element from the serialized storage: parameter_array_storage
    (whole-storage clone) + assoc_value_at (parse-cache hit + linear
    scan) — once per missed read. Probe counts at n=1000, 50 reads:
    hit = 0 storage fetches; miss = 50 storage fetches + 50 scans.
  • The same leg also re-evaluated a side-effecting subscript
    (assoc_subscript_key) a second time: declare -A m=([1]=one); x=${m[$((++i+5))]} left i=2 vs GNU i=1. This change restores
    single evaluation on the settled-miss path.

Fix (read side only)

  • New Executor::assoc_element_fetch_settled (src/executor/arrays/executor.rs)
    re-derives the fetch assoc branch's own admission on the same names:
    assoc-marked storage name (nameref-resolved), freshness gate open
    (!array_cells_dirty() || cell_is_fresh), structured assoc cell
    present, not the all-elements * form.
  • The operator/array handler answers a settled miss directly
    (Some("")) and skips the storage leg. Every state the fetch
    declines keeps the storage leg unchanged as the source of truth.
  • No write-path or mirror-sync changes; the VarTable write funnel
    (P-3) is a separate ruled lane and is untouched.

Behavior

GNU differential probes byte-identical (hit/miss values, set-but-empty
elements, unset-then-miss, :-/${#}/+ forms over a miss,
declare -p shape and order, function-local and nameref reads), with
one correction toward GNU: side-effecting subscripts on a miss now
evaluate once (was twice). Regression pins:
tests/perf_assoc_element_miss_regressions.rs (GNU bash 5.3.20
expectations).

Gates (to be filled in)

  • GNU differential probe byte-identical
  • diffcorpus 306/306
  • lib failure set identical to this tree's pristine baseline
  • re-measure under sampling gate (assoc_get_miss/hit vs GNU, interleaved)
  • -D warnings clean

A plain ${name[key]} expansion reaches the operator/array braced
handler only after the indexed handler's typed fetch returned None.
That handler re-derived assoc elements from the serialized storage:
a whole-storage clone plus the assoc_value_at linear scan per read,
which is Theta(n) per missed read (arrays.assoc_get_miss measured
13.96x GNU in the 2026-10-28 perf corpus; pristine probes: miss
245us vs GNU ~4us at n=10000), and it evaluated a side-effecting
subscript a second time where GNU evaluates it once per expansion
(subst.c:9777 parameter_brace_expand -> arrayfunc.c assoc_reference;
assoc.c assoc_reference is a single hash lookup, hit or miss).

The fetch's assoc branch already treats a miss as authoritative
whenever the freshness gate is open and a structured assoc cell
exists (rubash#616 Phase 1a invariant: the cell is a complete mirror
of the render). Add Executor::assoc_element_fetch_settled, which
re-derives exactly that admission on the same names, and let the
operator/array handler answer a settled miss directly. Every state
the fetch declines (unresolvable nameref, assoc mark absent, gate
closed, no assoc cell, the all-elements * form) keeps the storage
leg as the source of truth, unchanged.

Read side only; no write-path or mirror-sync changes (the VarTable
write funnel is a separate ruled lane).

Regression pins in tests/perf_assoc_element_miss_regressions.rs
assert GNU-observable behavior: hit/miss values after loop fill,
set-but-empty elements, unset-then-miss, single subscript
evaluation on hit and miss, operator/length forms over a miss,
declare -p shape after reads, function-local and nameref reads.
Expectations verified byte-for-byte against GNU bash 5.3.20.
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