On Mon, 1 May 2023 21:00:40 GMT, John R Rose <jr...@openjdk.org> wrote:

> HC fields are supposed to fold up; if they don’t we can help them in this 
> case. (That is, constant folding of the MH pointer should not be a roadblock; 
> that’s on the JIT to fix by adjusting the TrustFinalFields logic.)

The issue with this is that constant folding of a field only happens when to 
holder object is also constant, which is not necessarily the case. For the 
non-constant case, we have to rely on type profiling to get optimal performance.

(recapping) Let's say we have a call site to a method `foo` in interface `I`. 
But, the receiver value we see at this call site is only ever an instance of a 
class `C`. The JIT can see from the profile of that call site that we only ever 
see a receiver of type `C`, and then do a guarded inline of `C::foo`, giving us 
optimal performance. This works because the type `C` has a 1-to-1 mapping with 
the implementation that we inline into the call site.

However, in the case of method handles, there is no 1-to-1 mapping between the 
type of the receiver, `MethodHandle`, and the target method. Instead, the 
target method depends on a particular receiver _instance_. So, to make type 
profiling work, we need to introduce a 1-to-1 mapping with a type, hence we 
define a hidden class HC per method handle instance, where the MH instance is 
constant for that particular class. This creates the 1-to-1 mapping. Type 
profiling works for the HC type we define, we inline `HC::foo`, and find that: 
hey, in HC::foo's implementation, the target MH is constant, so we can inline 
through that as well. Giving us once again, optimal performance.

I've considered in the past that we could add _instance_ based profiling for 
MethodHandles, so that we can once again get a 1-to-1 mapping between a profile 
and a method we want to inline. But it wouldn't work in the case of 
MHP::asInterfaceInstance. Since profiles are tied to a BCI, it wouldn't work if 
we create multiple instances of the same HC for different MH instances, since 
the profile of the `invokeExact` call site we generate in the HC would be 
polluted by the different MH instances. (unless we implement: 
https://bugs.openjdk.org/browse/JDK-8015416)

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

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

Reply via email to