Memoize JsInliner.containsNestedFunctions - #10397
Open
rhuanhianc wants to merge 2 commits into
Open
Conversation
The predicate only depends on the function body, but it was recomputed by traversing that whole body once per candidate call site, on every inliner pass. Profiling a large application attributes about 79% of JsInliner's time to it. Cache it per function, dropping the entry wherever the inliner rewrites a body. On two applications JsInliner goes from 57.9s to 10.5s and from 328.5s to 22.8s, with byte-identical output.
zbynek
reviewed
Aug 19, 2026
| * Examine a JsFunction to determine if it contains nested functions. | ||
| */ | ||
| private static boolean containsNestedFunctions(JsFunction func) { | ||
| Boolean cached = containsNestedFunctionsCache.get().get(func); |
Collaborator
There was a problem hiding this comment.
You can use computeIfAbsent here.
Author
There was a problem hiding this comment.
Thanks, updated to use computeIfAbsent.
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.
containsNestedFunctionsis a pure function of a function's body, but it is recomputed — a full traversal of the entire body — once per candidate call site, on every inliner pass. On a large application, JFR sampling attributes about 79% of all JsInliner time to this single predicate, and JsInliner itself accounts for 28.7% of the permutation's CPU.This memoizes it in a thread-local
IdentityHashMap, cleared at the start of each execImpl so it never outlives a single run of this optimizer, and invalidated per function wherever the inliner rewrites that function's body.Invalidation happens at both points where a body actually changes: in
process()when an inline is committed, and again right after ctx.replaceMe(op). The second is necessary because the intervening op = accept(op) can re-populate the memo for the caller while op is still detached from the tree.endVisit(JsExprStmt)also mutates bodies, but only by removing or restructuring content already present in that statement, so it can only take the answer from true to false. That is the conservative direction: a stale true leavessafeScopenull and makes the inliner more cautious, never less.Measured on two real applications, JsInliner drops from 57.9s to 10.5s and from 328.5s to 22.8s. Generated JavaScript is byte-for-byte identical across 874 output files from four applications, including two built with two permutations (4 and 20 local workers).
ant -Dtarget=test devpasses: 1936 tests, 0 failures.Fixes #10394