Skip to content

Release memoized javac state when a compilation finishes - #6406

Open
rsalvador wants to merge 1 commit into
google:masterfrom
rsalvador:memoize-context-cache
Open

rsalvador wants to merge 1 commit into
google:masterfrom
rsalvador:memoize-context-cache

Conversation

@rsalvador

@rsalvador rsalvador commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

VisitorState.memoize and ClassName keep 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-private PerCompilationValue, so they live as long as their compilation:

  • The map holds its keys weakly, because some callers, like UnusedMethod, create suppliers per tree.
  • Misses still synchronize on the supplier and check again, so supplier calls are serialized as before. Null results are still not cached.
  • ClassName no longer needs VisitorState.javacInvocationInstance(), which is removed.
  • As an optimization, each PerCompilationValue also 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 core sources 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.

VisitorStateTest and ClassNameTest check that values are collected after their compilation and kept during it, and that memoize still serializes supplier calls and retries null results.

Fixes #6405

`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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VisitorState.memoize and ClassName caches keep finished compilations alive

1 participant