perf: use in-place Arrays.sort for std.sort/std.set/std.uniq#664
Closed
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Closed
perf: use in-place Arrays.sort for std.sort/std.set/std.uniq#664He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
e4fc0b5 to
21ab9c0
Compare
Replace map+sortBy chain with java.util.Arrays.sort for numeric, string, and array sorting in the identity-key path. This eliminates intermediate array allocations from the Schwartzian transform in Scala's sortBy and sorts the strict array in-place. Upstream: jit branch commit 926a6d0
21ab9c0 to
23ef1b1
Compare
Contributor
Author
|
Closing: Independent verification shows a 12% regression on setUnion (the primary sort benchmark). java.util.Arrays.sort with Comparator lambdas has more overhead per comparison than Scala's sortBy, which extracts keys once via map. The allocation savings don't offset the per-comparison overhead. Verified with Hyperfine (warmup 5, runs 20, --shell=none):
|
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
std.sortuses Scala'ssortWithwhich creates a new sorted copy. For homogeneous arrays (all numbers or all strings), we can use Java's optimizedArrays.sortin-place on a copied array, avoiding the overhead of Scala's generic sorting.Key Design Decision
Detect homogeneous arrays at sort time and dispatch to
Arrays.sort(Double[])orArrays.sort(String[])for maximum performance. Fall back to the generic sort for mixed-type arrays.Modification
Added homogeneous array detection in
SetModule.scalasortArrmethod. UsesArrays.sortwith primitive specialization for numeric and string arrays.Benchmark Results
JMH Regression Suite (1 fork, 3 warmup, 1 measurement)
All other benchmarks within noise margin.
Scala Native Hyperfine (
-N -w4 -m20)Analysis
Arrays.sortuses dual-pivot Quicksort which is highly optimized for primitive arrays. The improvement is most visible on sort-heavy benchmarks. On native, sjsonnet with this PR is now slightly faster than jrsonnet on bench.06.References
Upstream jit branch exploration at he-pin/sjsonnet@jit
Result
15% improvement on JMH bench.06, 10% on native. sjsonnet is now 1.05x faster than jrsonnet for array sorting, closing a previous 1.36x gap.