RFR: 8254354: Add an asExact() VarHandle combinator [v2]
Rémi Forax
github.com+828220+forax at openjdk.java.net
Mon Oct 26 18:57:43 UTC 2020
On Mon, 26 Oct 2020 16:34:10 GMT, Paul Sandoz <psandoz at openjdk.org> wrote:
>> The direct use of the enum ordinal is because HotSpot accessing it from the enum value is (or was) not optimal in C2.
>>
>> You can avoid the addition of the stable array by doing the following:
>>
>> public final MethodType accessModeType(AccessMode accessMode) {
>> return accessModeType(accessMode.at.ordinal());
>> }
>>
>> @ForceInline
>> final MethodType accessModeType(int accessModeOrdinal) {
>> TypesAndInvokers tis = getTypesAndInvokers();
>> MethodType mt = tis.methodType_table[accessModeOrdinal];
>> if (mt == null) {
>> mt = tis.methodType_table[accessModeOrdinal] =
>> accessModeTypeUncached(accessModeOrdinal);
>> }
>> return mt;
>> }
>>
>> final MethodType accessModeTypeUncached(int accessModeOrdinal) {
>> return accessModeTypeUncached(AccessMode.values()[accessModeOrdinal]);
>> }
>> It's a little odd going back and forth between the ordinal and the enum value, but it's all on the slow uncached path, and it avoids some duplication of code.
>>
>> I'll take a closer looks at the other areas and respond in another comment.
>
> We can clarify the new methods and tie them closer to method handle semantics. I suggest the names `asExactInvoker` and `asInvoker`, referencing `MethodHandles.exactInvoker` and `MethodHandles.invoker` respectively. (The term "generic" is really reserved for erased method types, and otherwise used internally as the generic invoker.)
>
> The `VarHandle` documentation already specifies that:
>
> * <p>
> * Invocation of an access mode method behaves as if an invocation of
> * {@link MethodHandle#invoke}, where the receiving method handle accepts the
> * VarHandle instance as the leading argument. More specifically, the
> * following, where {@code {access-mode}} corresponds to the access mode method
> * name:
> * <pre> {@code
> * VarHandle vh = ..
> * R r = (R) vh.{access-mode}(p1, p2, ..., pN);
> * }</pre>
> * behaves as if:
> * <pre> {@code
> * VarHandle vh = ..
> * VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
> * MethodHandle mh = MethodHandles.varHandleExactInvoker(
> * am,
> * vh.accessModeType(am));
> *
> * R r = (R) mh.invoke(vh, p1, p2, ..., pN)
> * }</pre>
> * (modulo access mode methods do not declare throwing of {@code Throwable}).
> ...
>
> We will need to adjust this section, i can help, but first let us reach agreement on this approach.
Paul,
an invoker has the MethodHandle (resp. VarHandle) as first argument so it's not the same semantics.
-------------
PR: https://git.openjdk.java.net/jdk/pull/843
More information about the core-libs-dev
mailing list