RFR: 6983726: Reimplement MethodHandleProxies.asInterfaceInstance [v22]

Jorn Vernee jvernee at openjdk.org
Tue Jul 11 13:43:26 UTC 2023


On Tue, 11 Jul 2023 13:16:21 GMT, Chen Liang <liach at openjdk.org> wrote:

> 1. I think the type profile pollution only happens with the instance-field approach, as I recall different instance fields' MHs pollute profiling. The comment need to be corrected if I'm right.

What happens is not really 'profile pollution'. The issue is that C2 can not inline through non-constant method handles. So we always get an indirect call through the MethodHandle's [invokeBasic stub](https://github.com/openjdk/jdk/blob/a1cfc9695405fe517fae1b9f760ae42b85f66be9/src/hotspot/cpu/x86/methodHandles_x86.cpp#L165).

> When this line is removed, the next call to asInterfaceInstance will return a wrapper with a new implementation class, even if the old instance is still in a local variable.

The WeakReference inside the implementation points to the `Lookup` object. I think this Lookup goes out of scope after `asInterfaceInstance` returns, right? (it's not saved inside the returned instance or class). So, it would be immediately eligible for GC.

I think a SoftReference would work better for the cache (which the GC clears less eagerly). Or the returned interface instance could save a reference to the Lookup object in order to keep it reachable.

> The MHP implementation class weak reference is not cleared by gc, even though the wrapper is no longer reachable.

Note that when running in interpreted mode (which is likely for these tests), a variable is 'alive' from the perspective of the GC until the method ends. i.e. the `{ ... }` used in the test does nothing WRT GC. You would have to explicitly make `c1` `null` for the class to be unreachable.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13197#issuecomment-1630856453


More information about the core-libs-dev mailing list