perf: optimize identity function composition#826
Merged
stephenamar-db merged 1 commit intoMay 11, 2026
Conversation
e3e480f to
19fb7ae
Compare
19fb7ae to
26bedd2
Compare
26bedd2 to
53556cf
Compare
Contributor
Author
|
Safety update pushed in
Validation:
|
8390d28 to
515691b
Compare
6d61e83 to
420a898
Compare
Motivation: bench.07 builds a deep chain of function(x) f(f(x)) over identity functions. Scala Native overflows the stack on this case with --max-stack 100000, and the JVM path creates tens of thousands of lazy values and function calls. Modification: Add an apply1 fast path for unary identity functions and recognize the exact non-tailstrict function(x) f(f(x)) shape. The wrapper preserves laziness, keeps explicit tailstrict eager semantics, and checks identity-composition chains iteratively instead of recursively. Result: bench.07 now passes on Scala Native, reduces the JVM debug counters from lazy_created=32786/function_calls=65550 to lazy_created=19/function_calls=16, and reports 0.036 ms/op in the single-case JMH run.
420a898 to
50403de
Compare
Contributor
Author
|
Rebased onto latest master and refreshed local validation/benchmark data in the PR description. Master still overflows on the Scala Native bench.07 case; the rebased PR passes and remains strongly positive. |
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:
bench/resources/cpp_suite/bench.07.jsonnetbuilds a lazy array of identity-equivalent composed functions. Current master evaluates the deep function/lazy chain normally; it is slow on JVM and still overflows Scala Native even with--max-stack 100000.Key Design Decision
Recognize only statically safe identity-equivalent unary functions and keep the runtime check conservative. Direct identity (
function(x) x) and the exact non-tailstrict self-composition shape (function(x) g(g(...g(x)))) can be elided only after the capturedgis proven effectively identity. Recursive composition cycles are treated as non-identity so normal max-stack behavior is preserved.Modification:
StaticOptimizer.EvaluatorintoVal.Funcinstances.isEffectivelyIdentityprobe with explicit Unknown/Yes/No/InProgress byte states.Benchmark Results:
JMH on JDK 21,
./mill -j 1 bench.runRegressions bench/resources/cpp_suite/bench.07.jsonnet, lowerms/opis better;ops/ms = 1 / ms_per_op, higher is better.cpp_suite/bench.07.jsonnetScala Native hyperfine, lower is better, comparing locally built Scala Native binary and source-built
jrsonnet 0.5.0-pre98.StackOverflowErrorwith--max-stack 100000-s 1000000)Analysis:
This optimization intentionally does not make arbitrary function equality assumptions. It only uses static shape tags plus runtime proof of the captured base function's effective identity. Errors and
tailstrictare preserved: constructingf2(error ...)remains lazy, calling it forces the original error, andtailstrictstill forces eagerly.References:
50403de6fd8689a4cefb3cd39d280821ed33ff7b0ae7b78a93c4e643f9bcfb6dd1d99d9fe7a522a9Result:
./mill --no-server -j 1 __.reformat && ./mill --no-server -j 1 __.testpassed locally../mill --no-server -j 1 'sjsonnet.native[3.3.7]'.nativeLinkpassed locally.