Skip to content

perf(array): negative-literal subscript fast lane via typed cell - #942

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/neg-subscript-fast
Oct 21, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/neg-subscript-fast

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Negative literal subscripts ([-1] etc) always missed typed-cell + forced full mirror parse (O(N2), 16k timed out >100s). Normalize via indexed_array_shape max (sparse-safe, GNU arrayfunc.c:1581 INDEX_ERROR) and probe typed cell; only clean+indexed+hit returns, everything else (assoc literal keys, dynamic/dirty/sparse-hole/diagnostics) falls back byte-identical. Tests: lib 664 green. 16k neg-subscript: 27.8s -> 585ms (47x), now 2.8x bash, at parity with positive-index fast lane.

`${a[-1]}` always missed the typed-cell probe (cells are keyed by
non-negative indices) and fell into expand+evalexp plus a mirror clone
and full re-parse per read: O(N) per read, O(N^2) for a 16k loop
(27.8s vs bash ~0.2s).

GNU arrayfunc.c:1581 array_value_internal normalizes a negative indexed
subscript as `array_max_index + 1 + ind` (INDEX_ERROR at :1582-1583 when
still negative); assoc subscripts never pass through evalexp
(arrayfunc.c:1604-1625, `-1` is a literal key). A clean `-[1-9][0-9]*`
literal has no names, expansions, RNG draws or writes, so it needs no
evalexp: normalize with indexed_array_shape's max (correct for sparse;
dense len == max+1 agrees) and hit indexed_element directly.

Whitelist admission only: clean+indexed+typed HIT returns; assoc bases,
dynamic views, dirty cells, empty arrays, still-negative results and
holes all fall through to the slow path, which owns every diagnostic
byte-for-byte. Leading-zero spellings stay slow (`-08` is an evalexp
octal error, `-0` is element 0).
@caomengxuan666
caomengxuan666 merged commit b31ab63 into master Oct 21, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/neg-subscript-fast branch October 21, 2026 08:45
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