Repository navigation
perf(array): negative-literal subscript fast lane via typed cell - #942
Merged
Merged
Conversation
`${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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.