You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2026/07/06 08:31:43 UTC

[PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

He-Pin opened a new pull request, #3300:
URL: https://github.com/apache/pekko/pull/3300

   ### Motivation
   `java.lang.reflect` `Method.invoke`, `Constructor.newInstance`, and `Field.get`/`set` bypass JIT inlining, adding overhead in critical paths like actor instantiation, protobuf serialization, and virtual thread management.
   
   ### Modification
   - **ReflectiveDynamicAccess**: Replace `Constructor.newInstance` with `MethodHandle.invokeWithArguments`; replace `Field.get(null)` for `MODULE$` with `VarHandle.get`
   - **Reflect**: Replace `Constructor.newInstance` with `MethodHandle.invoke` for both no-arg and parameterized constructors
   - **VirtualThreadSupport**: Replace `Field.set`/`get` with `VarHandle.set`/`get` for scheduler and `DEFAULT_SCHEDULER` fields; replace `Constructor.newInstance` with cached `MethodHandle` for `CarrierThread`
   - **ProtobufSerializer**: Replace `Method`-based `parseFrom`/`toByteArray` caches with `MethodHandle` caches for serialization hot path
   - **LineNumbers**: Replace `Method.invoke` with `MethodHandle.invoke` for `writeReplace` lambda introspection
   - **ByteBufferCleaner**: Replace `Field.get(null)` with `VarHandle.get` for `theUnsafe` static field access
   - **ExtensionsImpl**: Replace `Method.invoke(null)` with `MethodHandle.invoke` for Java singleton `getInstance` accessor
   
   ### Result
   `VarHandle` and `MethodHandle` enable JIT inlining of field access, method invocation, and constructor calls across all core reflection utilities, improving performance in actor creation, serialization, and virtual thread operations.
   
   ### Tests
   - `sbt "actor / compile"` - success
   - `sbt "actor-typed / compile"` - success
   - `sbt "remote / compile"` - success
   
   ### References
   None - internal refactoring


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3535907680


##########
actor-typed/src/main/scala/org/apache/pekko/actor/typed/internal/LoggerClass.scala:
##########
@@ -70,7 +71,7 @@ private[pekko] object LoggerClass {
             lambdaNameIdx = nextName.indexOf("$$Lambda")
             if nextName.startsWith(cls.getName()) && lambdaNameIdx > 0
             lambdaClsOwner = nextName.substring(0, lambdaNameIdx)
-          } yield Class.forName(lambdaClsOwner)
+          } yield Reflect.loadClass(nextCaller.getClassLoader, lambdaClsOwner)

Review Comment:
   Could this change behaviour? Class.forName(lambdaClsOwner) infers a classloader and is it definitely going to be the same as `nextCaller.getClassLoader`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3527512337


##########
actor/src/main/scala/org/apache/pekko/dispatch/VirtualThreadSupport.scala:
##########
@@ -100,11 +100,11 @@ private[dispatch] object VirtualThreadSupport {
       }
       // set the scheduler
       if (executor ne null) {
-        // Use reflection here, method handle is stricter on access control
         val clazz = builder.getClass
         val field = clazz.getDeclaredField("scheduler")
         field.setAccessible(true)

Review Comment:
   is field.setAccessible(true) needed now?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3527502998


##########
actor/src/main/scala/org/apache/pekko/actor/ReflectiveDynamicAccess.scala:
##########
@@ -74,8 +76,9 @@ class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAcces
       Try {
         val module = c.getDeclaredField("MODULE$")
         module.setAccessible(true)

Review Comment:
   the module.setAccessible(true) may not be necessary now



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3527499742


##########
actor/src/main/java/org/apache/pekko/io/ByteBufferCleaner.java:
##########
@@ -51,9 +51,10 @@ private static final class Java9Cleaner implements Cleaner {
 
     private Java9Cleaner() throws ReflectiveOperationException {
       final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
-      final Field field = unsafeClass.getDeclaredField("theUnsafe");
+      final java.lang.reflect.Field field = unsafeClass.getDeclaredField("theUnsafe");
       field.setAccessible(true);

Review Comment:
   the field.setAccessible(true) is probably not needed after these changes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3527513436


##########
actor/src/main/scala/org/apache/pekko/dispatch/VirtualThreadSupport.scala:
##########
@@ -100,11 +100,11 @@ private[dispatch] object VirtualThreadSupport {
       }
       // set the scheduler
       if (executor ne null) {
-        // Use reflection here, method handle is stricter on access control
         val clazz = builder.getClass
         val field = clazz.getDeclaredField("scheduler")
         field.setAccessible(true)
-        field.set(builder, executor)
+        val schedulerHandle = MethodHandles.lookup().unreflectVarHandle(field)

Review Comment:
   cache MethodHandles.lookup() value?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3527519367


##########
actor/src/main/scala/org/apache/pekko/actor/ReflectiveDynamicAccess.scala:
##########
@@ -74,8 +76,9 @@ class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAcces
       Try {
         val module = c.getDeclaredField("MODULE$")
         module.setAccessible(true)

Review Comment:
   Yes, the code is not right, still need rework.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3535860165


##########
remote/src/main/scala/org/apache/pekko/remote/serialization/ProtobufSerializer.scala:
##########
@@ -101,21 +98,20 @@ class ProtobufSerializer(val system: ExtendedActorSystem) extends BaseSerializer
 
   override def toBinary(obj: AnyRef): Array[Byte] = {
     val clazz = obj.getClass
-    @tailrec
-    def toByteArrayMethod(method: Method = null): Method = {
+    def toByteArrayMethod(): MethodHandle = {
       val toByteArrayMethodBinding = toByteArrayMethodBindingRef.get()
       toByteArrayMethodBinding.get(clazz) match {
-        case Some(cachedtoByteArrayMethod) => cachedtoByteArrayMethod
-        case None                          =>
-          val unCachedtoByteArrayMethod =
-            if (method eq null) clazz.getMethod("toByteArray")
-            else method
+        case Some(cachedHandle) => cachedHandle
+        case None               =>
+          val unCachedHandle = MethodHandles
+            .publicLookup()
+            .findVirtual(clazz, "toByteArray", MethodType.methodType(classOf[Array[Byte]]))

Review Comment:
   can we cache `MethodType.methodType(classOf[Array[Byte]])`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3535853229


##########
stream-tests/src/test/scala/org/apache/pekko/stream/DslConsistencySpec.scala:
##########
@@ -13,13 +13,15 @@
 
 package org.apache.pekko.stream
 
-import java.lang.reflect.Method
-import java.lang.reflect.Modifier
+import java.lang.invoke.MethodType
 
 import scala.annotation.nowarn
+import scala.jdk.CollectionConverters._
 
 import org.apache.pekko
 
+import _root_.io.github.classgraph.ClassGraph

Review Comment:
   do we need the `_root_`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3527516337


##########
actor/src/main/scala/org/apache/pekko/util/Reflect.scala:
##########
@@ -60,11 +61,14 @@ private[pekko] object Reflect {
    */
   private[pekko] def instantiate[T](clazz: Class[T]): T = {
     val ctor = clazz.getDeclaredConstructor()
-    try ctor.newInstance()
-    catch {
+    try {
+      val handle = MethodHandles.lookup().unreflectConstructor(ctor)

Review Comment:
   cache MethodHandles.lookup() value?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin merged PR #3300:
URL: https://github.com/apache/pekko/pull/3300


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #3300:
URL: https://github.com/apache/pekko/pull/3300#issuecomment-4890831435

   This pR is not ready, let's mark it to draft for now


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3535790490


##########
remote/src/main/scala/org/apache/pekko/remote/serialization/ProtobufSerializer.scala:
##########
@@ -74,25 +72,24 @@ class ProtobufSerializer(val system: ExtendedActorSystem) extends BaseSerializer
   override def fromBinary(bytes: Array[Byte], manifest: Option[Class[?]]): AnyRef = {
     manifest match {
       case Some(clazz) =>
-        @tailrec
-        def parsingMethod(method: Method = null): Method = {
+        def parsingMethod(): MethodHandle = {
           val parsingMethodBinding = parsingMethodBindingRef.get()
           parsingMethodBinding.get(clazz) match {
-            case Some(cachedParsingMethod) => cachedParsingMethod
+            case Some(cachedParsingHandle) => cachedParsingHandle
             case None                      =>
               checkAllowedClass(clazz)
-              val unCachedParsingMethod =
-                if (method eq null) clazz.getDeclaredMethod("parseFrom", ProtobufSerializer.ARRAY_OF_BYTE_ARRAY: _*)
-                else method
+              val unCachedHandle = MethodHandles
+                .publicLookup()

Review Comment:
   can we cache the publicLookup value?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3535873417


##########
actor/src/main/scala/org/apache/pekko/util/LineNumbers.scala:
##########
@@ -207,9 +207,11 @@ object LineNumbers {
   private def getStreamForLambda(l: AnyRef): Option[(InputStream, Some[String])] =
     try {
       val c = l.getClass
-      val writeReplace = c.getDeclaredMethod("writeReplace")
-      writeReplace.setAccessible(true)
-      writeReplace.invoke(l) match {
+      val writeReplaceHandle =
+        java.lang.invoke.MethodHandles
+          .privateLookupIn(c, java.lang.invoke.MethodHandles.lookup())

Review Comment:
   can we cache `java.lang.invoke.MethodHandles.lookup()`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] Replace java.lang.reflect with MethodHandle/VarHandle across core modules [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #3300:
URL: https://github.com/apache/pekko/pull/3300#discussion_r3527509863


##########
actor/src/main/scala/org/apache/pekko/actor/ReflectiveDynamicAccess.scala:
##########
@@ -74,8 +76,9 @@ class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAcces
       Try {
         val module = c.getDeclaredField("MODULE$")
         module.setAccessible(true)
+        val moduleHandle = MethodHandles.lookup().unreflectVarHandle(module)

Review Comment:
   is it worth caching the `MethodHandles.lookup()` value?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org