RFR: 8325949: Create an internal utility method for creating VarHandle instances [v3]
Per Minborg
pminborg at openjdk.org
Tue Sep 17 12:59:13 UTC 2024
On Mon, 16 Sep 2024 12:01:26 GMT, Per Minborg <pminborg at openjdk.org> wrote:
>> This PR suggests introducing an internal class in `java.base` to simplify the use of some `MethodHandles.Lookup` operations.
>>
>> While the utility of the methods might appear to be limited in classes with many static `VarHandle`/`MethodHandle` variables, it should be noted that the class files become smaller and simpler. Here are some examples:
>>
>> | Class File | Base [Bytes] | Patch [Byte] |
>> | --------------------------------| ------------- | ------------ |
>> | FutureTask.class | 10,255 | 10,123 |
>> | AtomicBoolean.class | 5,364 | 5,134 |
>> |AtomicMarkableReference.class | 3,890 | 3,660 |
>>
>> 
>>
>> The new `MethodHandlesInternal.class` file is of size 1,952 bytes.
>>
>> In total for `java.base` we have:
>>
>> | Build map "jdk" | Size [Bytes] |
>> | ---------------| ------------- |
>> | Base | 5,906,457 |
>> | Patch | 5,905,487 |
>> | Delta | 940|
>>
>> For 60 billion instances, this represents > 50 TB.
>>
>> Tried and passed tier1-3
>
> Per Minborg has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains five additional commits since the last revision:
>
> - Move to new package and add overload
> - Merge branch 'master' into internal-mh-util
> - Rename and reformat
> - Fix copyright headers
> - Introduce MethodHandlesInternal
It is possible to create a project-specific utility method that can unwrap several methods like this:
@FunctionalInterface
public interface LookupFunction<R> {
R apply(MethodHandles.Lookup lookup,
Class<?> refc,
String name,
MethodType type) throws NoSuchMethodException, IllegalAccessException;
}
public static <R> R unwrap(LookupFunction<R> lookupFunction,
MethodHandles.Lookup lookup,
Class<?> refc,
String name,
MethodType type) {
try {
return lookupFunction.apply(lookup, refc, name, type);
} catch (ReflectiveOperationException e) {
throw new InternalError(e);
}
}
static final MethodHandle MH_SCALE = MethodHandlesUtil.unwrap(MethodHandles.Lookup::findVirtual,
MethodHandles.lookup(), MemoryLayout.class, "scale", MethodType.methodType(long.class, long.class, long.class));
-------------
PR Comment: https://git.openjdk.org/jdk/pull/20972#issuecomment-2355690263
More information about the core-libs-dev
mailing list