Add lazy array views for large stdlib arrays#814
Open
He-Pin wants to merge 4 commits intodatabricks:masterfrom
Open
Add lazy array views for large stdlib arrays#814He-Pin wants to merge 4 commits intodatabricks:masterfrom
He-Pin wants to merge 4 commits intodatabricks:masterfrom
Conversation
6569dd0 to
be42d80
Compare
Contributor
Author
|
I think a SliceArr and RepeatedArr can be added in the later pr once this got merged to reduce the allocations |
Collaborator
|
please rebase |
Motivation: Reduce per-element thunk allocations and add cheap array paths to improve performance on hot lazy-array workloads. Modification: - Replace per-element LazyApply materialization with array-level lazy views - Add ReversedLazyViewArr and index-based evaluator/comprehension iteration - Add RangeArr/ByteArr fast-paths for sum/avg - Update stdlib call-sites to use arr.eval(i)/arr.value(i) Result: Significant wins on reverse-sparse and range_sum_avg benchmarks; minor, acceptable regressions on some composite workloads. Co-authored-by: Copilot <[email protected]>
65840c5 to
a7574ce
Compare
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.
Summary
This PR adds array-level lazy views for large
std.map,std.mapWithIndex, and non-constantstd.makeArrayresults.The old representation eagerly allocated one
LazyApply1/LazyApply2per element. For large arrays this burns allocation even when callers only force a small suffix/prefix. The new representation keeps the callback and evaluation context once per array, computes elements on demand, and caches computed values.Important JVM/JIT/GC choices:
Array[LazyApply*]representation belowLAZY_VIEW_THRESHOLD = 4096, where the JVM is usually better off with the simple monomorphic layout.Array[Eval]fromasLazyArrayso existing full-array stdlib consumers remain stack-safe and predictable.std.reverse(lazyView)materializes before reversing so the original and reversed arrays share the same element thunks; this preserves trace/native callback semantics and avoids duplicate evaluation.Correctness Coverage
Added directional tests for selective forcing and a trace regression covering the reverse-cache behavior for all three lazy view subclasses:
MakeArrayArrMappedArrMappedWithIndexArrAlso added
bench/resources/sjsonnet_suite/lazy_array_sparse_indexing.jsonnet, a reproducible sparse-indexing workload for the benchmark harness.JMH
Environment: JMH 1.37, Mill JVM
zulu:21,-wi 3 -i 5 -w 1s -r 2s -f 1 -tu ms. Baseline isupstream/masterat3a9a4928; benchmark files introduced by this PR were passed to master by absolute path for the baseline run. This sweep was rerun after the lazy-array hot-path fix in65840c5c.bench/resources/sjsonnet_suite/lazy_array_comprehension.jsonnet27.772 ± 9.548 ms/op19.945 ± 2.908 ms/opbench/resources/cpp_suite/realistic2.jsonnet44.530 ± 0.685 ms/op39.766 ± 0.661 ms/opbench/resources/go_suite/foldl.jsonnet0.070 ± 0.001 ms/op0.072 ± 0.001 ms/opbench/resources/sjsonnet_suite/lazy_array_sparse_indexing.jsonnet20.023 ± 4.123 ms/op3.488 ± 0.135 ms/opbench/resources/go_suite/reverse.jsonnet6.434 ± 0.224 ms/op5.001 ± 0.150 ms/opbench/resources/go_suite/base64_byte_array.jsonnet0.749 ± 0.037 ms/op0.748 ± 0.042 ms/opEarlier GC profiler on the directional lazy-view benchmark:
sjsonnet/test/resources/new_test_suite/lazy_array_views.jsonnet12,918,912.631 B/op1,728,840.113 B/opHyperfine vs jrsonnet
This is a Scala Native CLI comparison against the local jrsonnet binary at measurement time:
jrsonnet 0.5.0-pre98,80cd36a docs: abandon wide logo version. It is CLI wall-clock evidence, not JVM/JIT/GC evidence; JVM claims should be read from the JMH section above.Command template:
bench/resources/sjsonnet_suite/lazy_array_comprehension.jsonnet73.1 ± 1.1 ms125.1 ± 0.8 msbench/resources/cpp_suite/realistic2.jsonnet80.5 ± 0.9 ms93.9 ± 0.8 msbench/resources/go_suite/foldl.jsonnet3.3 ± 0.1 ms4.2 ± 0.1 msbench/resources/sjsonnet_suite/lazy_array_sparse_indexing.jsonnet14.0 ± 0.5 ms24.6 ± 0.7 msbench/resources/go_suite/reverse.jsonnet12.5 ± 0.5 ms18.7 ± 0.2 msbench/resources/go_suite/base64_byte_array.jsonnet5.8 ± 0.1 ms14.1 ± 0.2 msValidation
./mill -i 'sjsonnet.jvm[3.3.7].test'./mill -i '__.checkFormat'./mill -i 'sjsonnet.js[3.3.7].compile'./mill -i 'sjsonnet.native[3.3.7].nativeLink'git diff --checkupstream/master:lazy_array_comprehension,realistic2,foldl,lazy_array_sparse_indexing,reverse,base64_byte_arraysjsonnet nativevs localjrsonneton the same 6 regression workloads