Add lazy slice array view#821
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
Conversation
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
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.
Motivation
After #814, sjsonnet has lazy array views for large stdlib arrays, but array slicing and
std.remove/std.removeAtstill had paths that eagerly allocated newArray[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:
length,eval(i), andvalue(i)stay O(1)Modification
Add a lazy
SliceArrview and routeArr.sliced(...)through it when the slice is large enough or the source is already compact/view-backed.Changed behavior:
SliceArrinstead of eagerly copyingArray[Eval]std.removeandstd.removeAtreuse slice + concat viewsRangeArr,ByteArr, lazy indexed arrays, repeat, slice) can slice as O(1) viewsResult
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:lazy_array_slice_removerealistic2bench.06lazy_array_sparse_indexingScala Native hyperfine, compared with
upstream/master, using Scala Native binaries, not JVM jars:lazy_array_slice_removeslice-viewran 2.21 +/- 0.09 times faster thanupstream-master.External performance diff, against jrsonnet built from source at
80cd36awithcargo build --release -p jrsonnet(jrsonnet 0.5.0-pre98):lazy_array_slice_removeJIT / GC review:
SliceArrpreserves indexed laziness:eval(i)returns anEval;value(i)forces only the requested element.Array[Eval]; small slices still copy to avoid source-retention overhead.std.remove/std.removeAtreuse slice and concat views, avoiding large intermediate arrays.Rollback boundary:
References
Arr.copyEvalTo; Presize array copy consumers #823 presizes selected copy consumers.