perf: optimize std.range allocation and add staticNull singleton#669
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: optimize std.range allocation and add staticNull singleton#669He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Three targeted optimizations: 1. std.range: Replace Scala Range.map.toArray with direct while-loop array allocation. Avoids boxing through Range and .map intermediates. 2. Val.staticNull: Add singleton null value for runtime results where position information is not meaningful (e.g., if-then without else). Mirrors the existing staticTrue/staticFalse pattern. 3. visitIfElse: Use Val.staticNull instead of allocating new Val.Null instances for the implicit else branch. Upstream: jit branch commit c4ee6be Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Two allocation hot spots:
std.range: Uses(from to to).map(i => Val.Num(pos, i)).toArraywhich creates a ScalaRange, maps to an intermediate collection, then converts toArray. For large ranges, this is wasteful.if-thenwithoutelseallocates a freshVal.Null(e.pos). In hot loops with conditional logic, this creates garbage.Key Design Decision
std.range(0, 2147483647)now produces a proper error instead of silently wrapping)Val.staticNullsingleton follows the existingstaticTrue/staticFalsepatternModification
1.
std.rangewhile-loop with overflow protection2.
Val.staticNullsingletonFollowing the existing
staticTrue/staticFalsepattern, adds a singleton null for implicit-else results where position is not meaningful.3. PrettyYamlRenderer null guard
Added
current.currentFile != nullcheck insaveCurrentPos()to handle Position with null file (from staticNull and staticTrue/staticFalse).Benchmark Results
JMH Regression Suite (1 fork, 3 warmup, 1 measurement iteration)
All 35 benchmarks within ±5% noise margin. This is an incremental building-block optimization.
Scala Native Hyperfine
This optimization reduces allocation overhead for
std.rangeand implicit-else, which are building-block improvements that compound with other optimizations (comprehension scope reuse, foldl while-loops, etc.). In isolation, native impact is within noise.Analysis
The
std.rangewhile-loop andstaticNullsingleton are foundational improvements that reduce allocation pressure. While individual JMH impact is within noise, these changes enable downstream optimizations (e.g., comprehension scope reuse in PR #675/#686 benefits from reduced range allocation). The overflow protection with long arithmetic is a correctness improvement that prevents silent integer wrapping for edge cases.References
Upstream: jit branch commit
c4ee6be7(range optimization + staticNull). Overflow fix added for correctness.Upstream jit branch exploration at he-pin/sjsonnet@jit
Result
Incremental building-block optimization with no regressions. Reduces allocation overhead for
std.rangeand implicit-else null. Adds overflow protection for large ranges.