Skip to content

perf(assoc): typed-cell read for single-element assoc lookups - #937

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/assoc-typed-read
Oct 20, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/assoc-typed-read

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Mirror of #924 for assoc: dirty-gated HashMap O(1) probe skips full-string clone + linear scan; miss/dirty falls back. Wired minimum write-side sync (executor canonical/fast-append, compound funnel, unset element, arith dirty-mark). GNU assoc.c:120. Tests: lib 663 + full suite green (11 pre-existing env fails identical on baseline). 8k interleave: 84s -> 337ms (248x); vs bash 4.4x (write+expansion overhead remains).

GNU assoc.c:120 assoc_reference is a single hash_search over the assoc
table — O(1), never a walk of the whole table. The mirror path cloned
the whole storage string (parameter_array_storage) and linear-scanned
it (assoc_value_at), so every ${A[k]} read paid O(n); interleaved
write+read additionally defeated the content-addressed parse memo (the
mirror mutates each iteration), taking 8k pairs to ~84s.

Mirror the indexed typed-cell leg (#924): under the same dirty gate,
probe variables.associative_element (HashMap::get, O(1)) first; a hit
skips the mirror clone + linear scan, a miss or dirty array falls back
to the mirror path. Key expansion (assoc_subscript_key, with side
effects) still runs first; the debug_assert compares the cell against
an on-demand mirror fetch — release compiles it out, skipping the
per-read O(n) clone on a typed hit. The [*] whole-table leg is
untouched.

Write-side sync keeps the "complete or absent" invariant the indexed
project established: element writes (canonical + fast-append budget
path), the compound-assignment funnel (compound/append/scalar-"0"
binds), unset element, and the arithmetic parser context (dirty-mark —
no store access there, same as the indexed store). The compound-append
staleness was caught by the debug_assert in executor_tests part_016
before it could ship.
@caomengxuan666
caomengxuan666 merged commit 1efbe07 into master Oct 20, 2026
8 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 21, 2026
assoc interleaved 8k (`m[k$i]=v$i; x=${m[k$i]}`) is still ~4.2x GNU bash
after the #937 typed-cell read fix. Scaling is linear (4k->32k doubles
with N), so there is no O(n) left in the write path: the #618
append + `array_compact_budget` scheme already amortizes the canonical
render/dedupe/sync. The residual is per-op fixed cost; this trims the
assoc-owned slice only (the discarded general word expansion in
`expand_command_word` is the larger remaining item and is owned by the
expansion pipeline, not touched here).

- `assoc_storage_append_element`: write the quoted `[k]=v` segment
  straight into the mirror buffer instead of building a segment String, a
  `format!(" {segment}")` and two quote Strings per append. Bytes are
  identical to `format_assoc_storage` (pinned by a new unit test over the
  quoting classes: plain/space/quote/backslash/`]`/`` ` ``/`$`/empty).
- `upsert_typed_assoc_element`: one BTreeMap lookup plus a HashMap upsert
  instead of re-entering `set_associative_element` (name String alloc +
  second lookup), and reuse the stored key on the overwrite arm — GNU
  assoc.c `assoc_insert` overwrites in place.
- `prepare_array_element_assignment_word`: a subscript with no expansion
  syntax and no `[`/`]`/`=` returns None without running the
  side-effect-free `expand_subscript_string` pass (subst.c:11063 identity).
- `expand_subscript_string_inner`: skip the IFS_GLUE/E309/E401 marker-strip
  chain when the expanded key carries none of the three sentinels
  (`str::replace` always copies; GNU's dequote walks a marker-free C string
  without copying).
- budget spends only on an OVERWRITE: a new key adds no duplicate segment,
  so a pure fill never pays a canonical re-render, while an overwrite burst
  still folds every `budget` writes. A cell the append cannot sync reports
  `NoCell` and drops the budget so the next write re-syncs on the canonical
  path (the "complete or absent" invariant is preserved).

Measured (aarch64 Termux, release, best-of-9 rotated, N=8000):
interleaved 4.21x -> 3.56x bash; writes-only 4.25x -> 3.34x; reads
3.43x -> 3.02x.

Verification: `cargo test --lib` 672 green; assoc/array/assignment
integration set green; `cli_tests` green except the dirty-tree
version-banner test. The one assoc integration failure,
`oils_spec_assoc_append_regressions::assoc_nameref`, is a pre-existing
debug-assert failure (nameref -> assoc-element scalar write leaves the
typed cell stale) — byte-identical on origin/master, not caused here.
@caomengxuan666
caomengxuan666 deleted the fix/assoc-typed-read branch October 21, 2026 08:01
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