Skip to content

perf: freshness-gate the typed-cell verify and array read gates (#1510) - #1515

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix3/p3-pr
Oct 24, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix3/p3-pr

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes the O(n²) in #1510 — whose root cause was not the iteration path: the debug coherence check verify_typed_cells re-parsed and compared every structured array cell at every command boundary (Θ(total elements) per command), so even a scalar loop next to a large untouched array went quadratic (40k-element iteration: 516s wall in the #1264 desktop re-run).

The fix wires up the existing per-name freshness protocol (VarTable tracked/stale/mirror-epoch, the p2/#914 design): verify skips fresh cells and registers them after proving; the 7 construction points of element/whole assignments mark synced; four read gates accept fresh names; three debug mirror assertions only check non-fresh hits. Mirrors have no write path outside the VarTable choke point, which marks stale unconditionally — an unsynced-mirror bug is still caught in full at the next boundary. No array representation or carrier-byte changes.

Measured (debug, rusage CPU): 40k iteration 516s → 5.37s, scaling 1.38/2.72/5.37 at 10k/20k/40k (linear); per-element write fill 4000: 35.2→1.22s; assoc fill: 57.4→1.19s. Output byte-identical to GNU bash 5.3.20 across the semantics battery. Gates: lib 723/1 (baseline ownership env item only), 246 differential 244/2 = {gl08, ms10} ⊆ baseline, typed_cell_coherence_fuzz 2/2, array_store_parity 2/2, array_compound_quote_regressions 17/17 (cluster tree; lib re-verified on this rebased tree).

Known remainder (documented in the cluster report, out of scope here): direct $((a[i])) reads inside arithmetic are still Θ(n)/read — the arithmetic evaluator holds only the mirror table, no typed-store handle; that is a design-level follow-up. The absolute ~30× vs GNU at 40k is the frozen #682 per-command floor, not array shape.

Fixes #1510

"${a[@]}" iteration was O(n^2), but the quadratic was not in the
iteration or expansion path: the debug-only verify_typed_cells
re-proved every structured array cell against its mirror parse at
EVERY command boundary, so any loop whose body runs real commands
paid O(total array elements) per iteration while the array sat
untouched (40k elements: 516s wall vs GNU 0.24s; a debug-assertions
off build of the same tree collapses every shape to the no-array
control, which pins the mechanism).

Wire the existing per-name freshness protocol (VarTable
tracked/stale/mirror-epoch, landed earlier but never consumed)
into the three places p2s F1/F3 design intended:

- verify_typed_cells skips cells cell_is_fresh() vouches for and
  registers a cell after a full proof; every mirror write passes a
  VarTable choke point (no DerefMut) and marks a tracked name
  stale, so the unsynced-mirror-write class this net guards is
  still re-proven in full at the next boundary.
  verify_typed_cells_forced keeps the coherence fuzz at full
  strength.
- Write paths that build cell and mirror together mark the pair
  synced (note_typed_cell_synced): the three element-assignment
  convergences and the four whole-assignment convergences.
- Read gates admit a typed hit when the global dirty bit is clear
  OR the name is fresh, and the three debug mirror re-check
  asserts only run for non-fresh hits (a fresh hit re-derived its
  element from the mirror on every read - a duplicate O(n) proof).

GNU has a single ARRAY object (array.c array_reference /
array_insert are O(1)); the typed cell is rubashs projection of
it, so semantics are unchanged - the same scripts produce
byte-identical output, only the re-proof frequency changes.

40k iteration: 5.37 CPU-s, scaling x1.97/x1.98 per doubling
(linear); element-write fill of 4000: 35.2s -> 1.22s. lib tests
723/1 (baseline ownership env item only); 246 differential
244/2 = {gl08, ms10}; coherence fuzz 2/2 via the forced entry.

Residual (documented in clusters/p3/REPORT.md): arithmetic-
context element reads $((a[i])) still pay O(n) per read - the
arithmetic evaluator holds only the mirror table (no typed
store handle in parser context), an architecture-level fix
left to a dedicated change.
@caomengxuan666
caomengxuan666 merged commit 55ea110 into master Oct 24, 2026
6 of 8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix3/p3-pr branch October 30, 2026 01:41
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.

[perf] "${a[@]}" iteration is O(n²): 40k elements take 516s (GNU 0.24s)

1 participant