Skip to content

perf: optimize std.range allocation and add staticNull singleton#669

Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin:perf/range-allocation-staticnull
Open

perf: optimize std.range allocation and add staticNull singleton#669
He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin:perf/range-allocation-staticnull

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Apr 4, 2026

Motivation

std.range allocates an Array[Eval] filled with Val.Null placeholders that are immediately overwritten. The Val.Null object is also re-allocated for each null literal encountered.

Key Design Decision

  1. Use a staticNull singleton to avoid re-allocating Val.Null objects
  2. Optimize std.range array initialization

Modification

  • Val.scala: Added Val.staticNull singleton
  • ArrayModule.scala: Optimized range allocation to use pre-sized array

Benchmark Results

JMH (JVM, 3 iterations)

Benchmark Master (ms/op) This PR (ms/op) Change
bench.02 50.427 ± 38.906 45.222 ± 2.636 -10.3%
comparison2 85.854 ± 188.657 70.138 ± 8.319 -18.3%
realistic2 73.458 ± 66.747 67.660 ± 12.884 -7.9%

References

  • Upstream exploration: he-pin/sjsonnet jit branch range/null optimization

Result

-10% to -18% JVM improvement by reducing allocation in range creation and null literals.

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>
@He-Pin He-Pin marked this pull request as ready for review April 4, 2026 21:42
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