Skip to content

perf: avoid full-list copies for indexed var lookups - #71

Open
siddarthaboddu wants to merge 1 commit into
jamsesso:mainfrom
siddarthaboddu:codex/optimize-indexed-var
Open

siddarthaboddu wants to merge 1 commit into
jamsesso:mainfrom
siddarthaboddu:codex/optimize-indexed-var

Conversation

@siddarthaboddu

Copy link
Copy Markdown

Looking up {"var":42} in an ArrayList previously copied and normalized every element to return one value. Numeric and dotted lookups such as {"var":"items.42"} now read the indexed element directly from Java arrays and RandomAccess lists, then normalize that value. Other collection types continue to use the existing lookup path.

Tests

All 112 tests passed on Java 8 and Java 21. A regression test checks that both rule forms return the value at index 42 with exactly one element read from a 64-element list. Another test covers primitive array access and the default value for a missing index.

Benchmark

Average time and allocation per call to JsonLogic.apply with the cached rule {"var":0} and a prebuilt ArrayList<Integer>.

Elements Time before Time after Allocated before Allocated after
1 74 ns 31 ns 472 B 120 B
16 201 ns 31 ns 1.0 KB 120 B
256 1.9 µs 30 ns 11 KB 120 B
4,096 27 µs 30 ns 149 KB 120 B
65,536 509 µs 33 ns 2.43 MB 120 B

Compared 49995a7 and baa05eb using JMH 1.37 on Corretto 21 with one thread, two JVM forks, a fixed 512 MB heap, and the GC profiler. Each fork ran two 500 ms warmup iterations and three 500 ms measurement iterations.

Read and normalize only the selected element for numeric indexes and
dotted paths. Keep the ArrayLike fallback for other iterable inputs.

Add read-count regression coverage for both lookup forms and verify
numeric conversion and missing-index handling on primitive arrays.

This branch has not been deployed

No deployments
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