Skip to content

Add lazy slice array view#821

Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin:perf/slice-array-view
Open

Add lazy slice array view#821
He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin:perf/slice-array-view

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented May 5, 2026

Motivation

After #814, sjsonnet has lazy array views for large stdlib arrays, but array slicing and std.remove / std.removeAt still had paths that eagerly allocated new Array[Eval] backing arrays.

This PR adds a focused slice view so large slices avoid copying unless fully materialized, while keeping JVM/Scala Native behavior conservative.

Constraints:

  • keep Jsonnet indexed laziness: length, eval(i), and value(i) stay O(1)
  • do not force elements while slicing
  • keep small slices eager to avoid retaining large sources
  • prevent deep concat trees
  • keep hot paths JIT/GC friendly

Modification

Add a lazy SliceArr view and route Arr.sliced(...) through it when the slice is large enough or the source is already compact/view-backed.

Changed behavior:

  • array slicing can return SliceArr instead of eagerly copying Array[Eval]
  • std.remove and std.removeAt reuse slice + concat views
  • compact sources (RangeArr, ByteArr, lazy indexed arrays, repeat, slice) can slice as O(1) views
  • flat/reversed/concat arrays only use a view when the slice is large enough to justify source retention
  • concat still caps tree depth by flattening when either side is already a concat view

Result

Verification passed:

  • ./mill --no-server 'sjsonnet.jvm[3.3.7].compile'
  • ./mill --no-server 'sjsonnet.jvm[2.13.18].compile'
  • ./mill --no-server 'sjsonnet.jvm[2.12.21].compile'
  • ./mill --no-server 'sjsonnet.jvm[3.3.7].test.testOnly' sjsonnet.ValArrayViewTests sjsonnet.Std0150FunctionsTests
  • ./mill --no-server 'sjsonnet.jvm[3.3.7].test'
  • ./mill --no-server 'sjsonnet.jvm[3.3.7].checkFormat'
  • ./mill --no-server bench.checkFormat
  • ./mill --no-server 'sjsonnet.native[3.3.7].nativeLink'

JMH, JVM harness, compared with upstream/master:

Benchmark Before After
lazy_array_slice_remove 5.890 ms/op 1.089 ms/op
realistic2 44.831 ms/op 40.899 ms/op
bench.06 0.144 ms/op 0.143 ms/op
lazy_array_sparse_indexing 3.569 ms/op 3.556 ms/op

Scala Native hyperfine, compared with upstream/master, using Scala Native binaries, not JVM jars:

Benchmark Before After
lazy_array_slice_remove 13.6 ms +/- 0.3 ms 6.2 ms +/- 0.2 ms

slice-view ran 2.21 +/- 0.09 times faster than upstream-master.

External performance diff, against jrsonnet built from source at 80cd36a with cargo build --release -p jrsonnet (jrsonnet 0.5.0-pre98):

Benchmark sjsonnet Scala Native (#821) source-built jrsonnet Result
lazy_array_slice_remove 5.8 ms +/- 0.2 ms 7.0 ms +/- 0.2 ms sjsonnet 1.21 +/- 0.05x faster

JIT / GC review:

  • SliceArr preserves indexed laziness: eval(i) returns an Eval; value(i) forces only the requested element.
  • Materializing a slice releases the source reference, so long-lived fully-consumed slices do not keep the original array alive.
  • Large slices avoid allocating and copying Array[Eval]; small slices still copy to avoid source-retention overhead.
  • std.remove / std.removeAt reuse slice and concat views, avoiding large intermediate arrays.
  • Concat depth remains bounded.

Rollback boundary:

  • This PR only changes slice/remove array representation.
  • If a retained-source workload regresses, the slice threshold is the rollback lever without changing the public API.

References

Motivation:

Avoid copying large array slices and remove/removeAt intermediates after the lazy-array work. This follows jrsonnet's indexed slice-view idea while keeping JVM retention under control for small sub-slices.

Modifications:

- add Val.Arr.sliced and SliceArr for large or compact-source slices
- route array slicing and std.remove/removeAt through slice/concat views
- let large concat decisions use total length, with overflow protection
- add correctness coverage and a slice/remove benchmark resource

Results:

- ./mill --no-server 'sjsonnet.jvm[3.3.7].compile'
- ./mill --no-server 'sjsonnet.jvm[2.13.18].compile'
- ./mill --no-server 'sjsonnet.jvm[2.12.21].compile'
- ./mill --no-server 'sjsonnet.jvm[3.3.7].test.testOnly' sjsonnet.ValArrayViewTests sjsonnet.Std0150FunctionsTests
- ./mill --no-server 'sjsonnet.jvm[3.3.7].test'
- ./mill --no-server 'sjsonnet.jvm[3.3.7].checkFormat'
- ./mill --no-server bench.checkFormat
- JMH runRegressions: lazy_array_slice_remove 5.890 -> 1.089 ms/op
- hyperfine macro slice/remove: 498.6 ms -> 335.5 ms
@He-Pin He-Pin mentioned this pull request May 5, 2026
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