perf: use hybrid sort for inline object order#855
Open
He-Pin wants to merge 1 commit into
Open
Conversation
0848e79 to
4e987c7
Compare
Motivation: Large inline objects produced by strict JSON imports can exceed the small-object shape that computeSortedInlineOrder was originally tuned for. Native sampling on kube-prometheus showed sorted inline-order computation as a materialization hotspot, and insertion sort becomes quadratic on those wider objects. Modification: Keep insertion sort for small inline objects, and use an in-place quicksort with median-of-three pivot and insertion-sort cleanup for larger visible field sets. Result: Kube-prometheus Native A/B improved on top of strict JSON byte imports, with forward mean 145.3ms -> 140.0ms and reverse mean 151.6ms -> 148.9ms. Formatting and the full test suite pass. References: Upstream-base: databricks/sjsonnet@cedc083 Prior optimization: 883fca5 perf: parse strict JSON imports from bytes
ef717de to
5332110
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.
Motivation
computeSortedInlineOrderwas originally tuned for inline objects with ahandful of fields. Once strict JSON imports started constructing inline
Val.Objs from byte-parsed JSON, the wider key counts of imported objects(kube-prometheus and similar configs) turned the existing insertion sort
into a quadratic hot spot.
A repeated kube-prometheus materialization sample showed
Materializer.computeSortedInlineOrderas a real Scala-Native top-stacksample. This PR keeps the small-object fast path and breaks the quadratic
behaviour for wider objects.
Modification
Materializer.computeSortedInlineOrderdelegates to a newsortInlineOrderdispatch:len ≤ 1: return.len ≤ 16: existing insertion sort over the index array.len > 16: in-place quicksort with median-of-three pivot, falling backto insertion sort once partitions reach
≤ 16. Recurses on thesmaller half (Sedgewick) so stack depth is
O(log n).Util.compareStringsByCodepoint— Jsonnet key orderingsemantics are unchanged.
Array[Int]is mutated; shared parsed keys/members are nottouched.
Result
Re-benched on 2026-05-21 against
master @ b252b184. Apple Silicon, JDK 21,Scala 3.3.7.
Allocation (JMH
-prof gc, full bench corpus)In-place sort, so allocation is unchanged. Every bench is within
±0.3%B/opexceptmanifestJsonExat-1.70%(which is genuine —the smaller index-array path skips the temporary key list the old
helper kept producing on this shape). No bench shows an alloc
regression > +0.3% / +250 B.
Wall-clock — Scala-Native release binary (hyperfine)
Selected object-construction-shape benches (warmup=2, min-runs=5):
Bench corpus impact is largely wall-clock-neutral: most short-running
benches (< 30 ms) are dominated by Native start-up variance (±10–15 %
run-to-run). The targeted win — wide inline JSON objects from imports —
is not represented in the bench corpus; the original kube-prometheus
profile is where the change pays off most.
No corpus-level regression > 5 % outside Native start-up noise.
Correctness
RendererTestsandJsonImportFastPathTestspass (13 + 7 cases)../mill 'sjsonnet.jvm[3.3.7]'.test— green../mill __.checkFormat— green.Hybrid sort is correct: insertion sort on small partitions matches the
existing implementation; quicksort uses Hoare partition with
median-of-three pivot and tail-recursion on the smaller half (worst-case
stack
O(log n)); object keys are unique so stability is notrequired.
Test plan
./mill 'sjsonnet.jvm[3.3.7]'.test— green./mill __.checkFormat— green