couple questions on method handle combinators

Vladimir Ivanov vladimir.x.ivanov at oracle.com
Wed Jun 14 17:36:15 UTC 2017


>>
>> There's a simpler solution: since MH.asType() already uses 1-element cache [1], it should be enough to just mark MH.asTypeCache as @Stable.
>>
>
> Would not invalidate the contract of @Stable where the value changes at most once? thereby causes issues for already compiled methods treating that as a constant when other code updates the value via an asType call.

> i realize that we break similar rules for updating the LF, but that is a special case where we are in control where as we don’t control who calls MH.asType.

It does break @Stable contract, but the JVM doesn't forbid multiple 
updates to @Stable fields and it's a benign case: keeping a stale value 
"cached" in compiled code doesn't break asType() caching logic. (For 
user it looks like the cache is always stale and MH.asType() should be 
recomputed.)

Best regards,
Vladimir Ivanov

>> The problem with MH.asType() is more general: 1-element caching scheme doesn't scale for multiple MH.invoke() call sites with different signatures. Every cache miss requires a new method handle to be created.
>>
>> Best regards,
>> Vladimir Ivanov
>>
>> [1] jdk/src/java.base/share/classes/java/lang/invoke/MethodHandle.java:
>> ...
>> public abstract class MethodHandle {
>> ...
>>    /*private*/ MethodHandle asTypeCache;
>> ...
>>    public MethodHandle asType(MethodType newType) {
>>        // Fast path alternative to a heavyweight {@code asType} call.
>>        // Return 'this' if the conversion will be a no-op.
>>        if (newType == type) {
>>            return this;
>>        }
>>        // Return 'this.asTypeCache' if the conversion is already memoized.
>>        MethodHandle atc = asTypeCached(newType);
>>        if (atc != null) {
>>            return atc;
>>        }
>>        return asTypeUncached(newType);
>>    }
>>
>>    private MethodHandle asTypeCached(MethodType newType) {
>>        MethodHandle atc = asTypeCache;
>>        if (atc != null && newType == atc.type) {
>>            return atc;
>>        }
>>        return null;
>>    }
>


More information about the valhalla-dev mailing list