perf: optimize std.makeArray for constant-body functions#697
Draft
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Draft
perf: optimize std.makeArray for constant-body functions#697He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
When std.makeArray receives a function whose body is a constant literal (e.g. function(_) 'x'), skip per-element LazyApply1 thunk allocation and Val.Num index creation. Instead, fill the entire array with the constant value using java.util.Arrays.fill. Safety guards: - Only applies when func.params.names.length == 1 (preserves arity checks) - Only applies when bodyExpr is a Val.Literal (Str/Num/Bool/Null/Arr/Obj) - bodyExpr is only set by Evaluator.visitMethod, builtins default to null Benchmark: -61.8% on large_string_join (1.902 → 0.726 ms/op) Upstream: jit branch commit 38ce4aa
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.makeArray(n, func)always evaluatesfunc(i)lazily for each index, even when the function body is a constant expression (e.g.,std.makeArray(100, function(i) 0)). This creates unnecessary thunk objects.Key Design Decision
Detect when the function body does not reference its parameter and optimize to fill the array with the constant value directly, avoiding per-element thunk allocation.
Modification
std.makeArrayimplementationBenchmark Results
JMH (JVM, 3 iterations warmup + 3 measurement)
Analysis
The realistic2 improvement (-11.0%) is notable because many Jsonnet programs use
std.makeArraywith constants for initialization. The very tight error bars (±2.2, ±2.3) confirm stable improvement.References
38ce4aa9Result
All 5 tests pass. All benchmarks positive, no regressions.