Skip to content

Add lazy array views for large stdlib arrays#814

Open
He-Pin wants to merge 4 commits intodatabricks:masterfrom
He-Pin:perf/lazy-array-architecture
Open

Add lazy array views for large stdlib arrays#814
He-Pin wants to merge 4 commits intodatabricks:masterfrom
He-Pin:perf/lazy-array-architecture

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Apr 30, 2026

Summary

This PR adds array-level lazy views for large std.map, std.mapWithIndex, and non-constant std.makeArray results.

The old representation eagerly allocated one LazyApply1/LazyApply2 per 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:

  • Keep the old flat Array[LazyApply*] representation below LAZY_VIEW_THRESHOLD = 4096, where the JVM is usually better off with the simple monomorphic layout.
  • Materialize back to Array[Eval] from asLazyArray so 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.
  • Release captured evaluator/function state after full materialization.

Correctness Coverage

Added directional tests for selective forcing and a trace regression covering the reverse-cache behavior for all three lazy view subclasses:

  • MakeArrayArr
  • MappedArr
  • MappedWithIndexArr

Also 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 is upstream/master at 3a9a4928; 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 in 65840c5c.

Benchmark path master this PR Result
bench/resources/sjsonnet_suite/lazy_array_comprehension.jsonnet 27.772 ± 9.548 ms/op 19.945 ± 2.908 ms/op 1.39x faster
bench/resources/cpp_suite/realistic2.jsonnet 44.530 ± 0.685 ms/op 39.766 ± 0.661 ms/op 1.12x faster
bench/resources/go_suite/foldl.jsonnet 0.070 ± 0.001 ms/op 0.072 ± 0.001 ms/op neutral
bench/resources/sjsonnet_suite/lazy_array_sparse_indexing.jsonnet 20.023 ± 4.123 ms/op 3.488 ± 0.135 ms/op 5.74x faster
bench/resources/go_suite/reverse.jsonnet 6.434 ± 0.224 ms/op 5.001 ± 0.150 ms/op 1.29x faster
bench/resources/go_suite/base64_byte_array.jsonnet 0.749 ± 0.037 ms/op 0.748 ± 0.042 ms/op neutral

Earlier GC profiler on the directional lazy-view benchmark:

Benchmark path master alloc this PR alloc Result
sjsonnet/test/resources/new_test_suite/lazy_array_views.jsonnet 12,918,912.631 B/op 1,728,840.113 B/op 86.6% less allocation

Hyperfine 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:

hyperfine -N --warmup 20 --runs 50 \
  -n 'sjsonnet native' \
  'out/sjsonnet/native/3.3.7/nativeWorkdir.dest/native/sjsonnet.SjsonnetMain <benchmark.jsonnet>' \
  -n 'jrsonnet' \
  '/Users/hepin/IdeaProjects/sjsonnet/jrsonnet/target/release/jrsonnet <benchmark.jsonnet>'
Benchmark path sjsonnet native jrsonnet Result
bench/resources/sjsonnet_suite/lazy_array_comprehension.jsonnet 73.1 ± 1.1 ms 125.1 ± 0.8 ms 1.71x faster
bench/resources/cpp_suite/realistic2.jsonnet 80.5 ± 0.9 ms 93.9 ± 0.8 ms 1.17x faster
bench/resources/go_suite/foldl.jsonnet 3.3 ± 0.1 ms 4.2 ± 0.1 ms 1.25x faster
bench/resources/sjsonnet_suite/lazy_array_sparse_indexing.jsonnet 14.0 ± 0.5 ms 24.6 ± 0.7 ms 1.76x faster
bench/resources/go_suite/reverse.jsonnet 12.5 ± 0.5 ms 18.7 ± 0.2 ms 1.49x faster
bench/resources/go_suite/base64_byte_array.jsonnet 5.8 ± 0.1 ms 14.1 ± 0.2 ms 2.42x faster

Validation

  • ./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 --check
  • JMH regression sweep on this PR and upstream/master: lazy_array_comprehension, realistic2, foldl, lazy_array_sparse_indexing, reverse, base64_byte_array
  • Hyperfine: sjsonnet native vs local jrsonnet on the same 6 regression workloads

@He-Pin He-Pin force-pushed the perf/lazy-array-architecture branch from 6569dd0 to be42d80 Compare April 30, 2026 16:56
@He-Pin He-Pin marked this pull request as draft April 30, 2026 17:43
@He-Pin He-Pin marked this pull request as ready for review April 30, 2026 19:36
@He-Pin
Copy link
Copy Markdown
Contributor Author

He-Pin commented Apr 30, 2026

I think a SliceArr and RepeatedArr can be added in the later pr once this got merged to reduce the allocations

@stephenamar-db
Copy link
Copy Markdown
Collaborator

please rebase

He-Pin and others added 4 commits May 3, 2026 01:10
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]>
@He-Pin He-Pin force-pushed the perf/lazy-array-architecture branch from 65840c5 to a7574ce Compare May 2, 2026 17:31
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.

2 participants