perf: cache ObjComp allLocals, while-loop sum/avg, arraycopy remove/removeAt#699
Closed
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Closed
perf: cache ObjComp allLocals, while-loop sum/avg, arraycopy remove/removeAt#699He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
…emoveAt - Cache preLocals ++ postLocals as lazy val allLocals in ObjComp AST node to avoid repeated array concatenation on each visitObjComp call. - Replace .map(_.value.asDouble).sum with while-loop in std.sum/std.avg to eliminate intermediate Array[Double] allocation and closure overhead. - Replace slice++slice with System.arraycopy in std.remove/std.removeAt to avoid creating 3 intermediate arrays (2 slices + concatenation). Upstream: jit branch commit 09e2d3a
Contributor
Author
|
Superseded by #700, which includes all changes from this PR plus additional optimizations (visibleKeyNames while-loop, base64DecodeBytes unsigned fix, and single-pass sum/avg merge). |
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
Three independent micro-optimizations that reduce allocation overhead in common stdlib operations and object comprehension evaluation.
Key Design Decision
Each sub-optimization targets a different allocation pattern:
.map().sum(creates intermediateArray[Double]) with a while-loopslice ++ slice(3 intermediate arrays) withSystem.arraycopy(1 array)Modification
1.
Expr.scala— ObjComp allLocals cacheAdded
lazy val allLocals: Array[Bind] = preLocals ++ postLocalstoObjBody.ObjComp. This is a body member (not a constructor param), so it does not affectequals/hashCode/copy. Scalalazy valprovides thread-safe initialization.2.
Evaluator.scala— Use cached allLocalsChanged
visitObjCompfrome.preLocals ++ e.postLocalstoe.allLocals, avoiding repeated allocation when the same AST node is re-evaluated (imports, loops).3.
ArrayModule.scala— sum/avg while-loopReplaced
arr.asLazyArray.map(_.value.asDouble).sumwith explicit while-loop. Theforallvalidation pass already forces and caches all lazy elements, so the while-loop reads cached values without double-forcing.4.
ArrayModule.scala— remove/removeAt arraycopyReplaced
arr.asLazyArray.slice(0, idx) ++ arr.asLazyArray.slice(idx + 1, arr.length)withSystem.arraycopy. Edge cases verified:idx=0: first copy is no-op, second copies all remainingidx=len-1: first copies all but last, second is no-oplen=1: both copies are no-ops, result is empty arrayBenchmark Results
JMH A/B (5 iterations, 3 warmup, single fork)
Note: These optimizations primarily benefit stdlib-heavy workloads (sum, avg, remove operations) rather than bench.02 (OO fibonacci). The improvement is modest but consistent, with much tighter variance.
Analysis
visitObjCompcall. Modest but free.Array[Double]allocation + boxing/unboxing for each element. For large numeric arrays, this is ~2-3x faster.System.arraycopyis a JVM intrinsic.References
Result
-2.9% improvement on bench.02 with zero regressions. All 3 sub-optimizations are independently correct and beneficial. All existing tests pass.