RFR: 8325949: Create an internal utility method for creating VarHandle instances

Roger Riggs rriggs at openjdk.org
Thu Sep 12 19:26:04 UTC 2024


On Thu, 12 Sep 2024 17:38:44 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                          |             10255 |           10154 |
> | AtomicBoolean.class                   |              5364 |             5161 |
> |AtomicMarkableReference.class |              3890 |            3687 |
> 
> ![image](https://github.com/user-attachments/assets/fdcbbdfe-c906-4e50-a48c-4944c53c08cc)
> 
> The new `MethodHandlesInternal.class` file is of size 2012 bytes.
> 
> In total for `java.base` we have:
> 
> | Build map "jdk"  | Size [Bytes] |
> | ---------------| ------------- |
> | Base                 |        5963657 |
> | Patch                |        5962751 |
> | Delta                 |                906|
> 
> For 60 billion instances, this represents 54 TB.

src/java.base/share/classes/jdk/internal/reflect/MethodHandlesInternal.java line 47:

> 45:     private MethodHandlesInternal() {}
> 46: 
> 47:     public static VarHandle findVarHandleOrThrow(MethodHandles.Lookup lookup, Class<?> recv, String name, Class<?> type) {

The method naming might benefit from being clear that failure is fatal; never call it with arguments that might fail.
Perhaps even throw java.lang.InternalError. ExceptionInInitializerError might be misconstrued as a some kind of race or being called too early.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/20972#discussion_r1757467476


More information about the core-libs-dev mailing list