Repository navigation
Conversation
`VisitorState.memoize` suppliers and `ClassName` instances are usually held in static fields. Both kept the javac state of the last compilation that used them in a `SoftReference`, and replaced it lazily when a later compilation used them. That state (`Type`s, `Symbol`s, `Name`s) references the whole compilation. So in a JVM that runs many compilations, completed compilations stay reachable until the soft references are cleared. Store the state in a map owned by the compilation's `Context` instead (`PerCompilationValue`), keyed weakly by the supplier or `ClassName`: - Values are never shared between compilations and are collected with them. - Unlike with weak references, values stay alive while the compilation runs. - A weak reference to the last value, tagged with the id of its compilation, keeps repeated lookups cheap. - Misses still compute one value at a time, so suppliers do not have to be thread-safe. - Null results are still not cached, and memoized suppliers still cannot access the `TreePath`.
This branch has not been deployed
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.
VisitorState.memoizeandClassNamekeep their values through soft references, in objects that are usually static constants, so in a long-lived JVM they keep finished compilations reachable until the JVM clears soft references.This change keeps those values in a map owned by the compilation's
Context, through a new package-privatePerCompilationValue, so they live as long as their compilation:UnusedMethod, create suppliers per tree.ClassNameno longer needsVisitorState.javacInvocationInstance(), which is removed.PerCompilationValuealso keeps a weak reference to its last value, tagged with the id of its compilation, so a repeated lookup in the same compilation skips the map.Values are now held strongly while their compilation runs. Weak references would also release finished compilations, but a GC during a compilation would then drop values it still uses, which is why these caches use soft references (c308666). Unlike soft references, strong ones can't be dropped under memory pressure, but the values are mostly symbols and types that the compilation holds anyway.
With the reproduction from #6405, no compilation stays reachable after GC, instead of 6 of 6. The larger run keeps 13 MB of heap after GC instead of 179 MB. In the JavaBuilder worker from #6405, none stays reachable instead of 18, with 0.01 GiB of heap after GC instead of 0.35 GiB. Compiling Error Prone's own
coresources 12 times in one JVM, as in #6078, took 5.47 to 5.49 s against 5.42 s on master (103086b, medians of 6 JVMs each, in two runs), about 1% more.VisitorStateTestandClassNameTestcheck that values are collected after their compilation and kept during it, and thatmemoizestill serializes supplier calls and retries null results.Fixes #6405