This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 4d87c32c81 Cache the result of `findMethod`
4d87c32c81 is described below

commit 4d87c32c81545eb6e2d3c884dcf6fddd70f79a75
Author: Daniel Sun <[email protected]>
AuthorDate: Sun Jan 5 06:37:05 2025 +0900

    Cache the result of `findMethod`
---
 src/main/java/groovy/lang/MetaClassImpl.java | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java 
b/src/main/java/groovy/lang/MetaClassImpl.java
index 549f8e1a11..8a4089aa2e 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -57,6 +57,7 @@ import org.codehaus.groovy.runtime.callsite.PojoMetaClassSite;
 import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;
 import org.codehaus.groovy.runtime.callsite.StaticMetaClassSite;
 import org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite;
+import org.codehaus.groovy.runtime.memoize.LRUCache;
 import org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod;
 import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
 import org.codehaus.groovy.runtime.metaclass.MetaMethodIndex;
@@ -3165,9 +3166,28 @@ public class MetaClassImpl implements MetaClass, 
MutableMetaClass {
         list.add(method);
     }
 
+    private static final MetaMethod NULL_METAMETHOD = new MetaMethod() {
+        @Override
+        public int getModifiers() { return 0; }
+        @Override
+        public String getName() { return ""; }
+        @Override
+        public Class getReturnType() { return null; }
+        @Override
+        public CachedClass getDeclaringClass() { return null; }
+        @Override
+        public Object invoke(Object object, Object[] arguments) { return null; 
}
+    };
+    private static final LRUCache<Method, MetaMethod> METHOD_CACHE = new 
LRUCache<>(512);
     private MetaMethod findMethod(Method method) {
-        CachedMethod cachedMethod = CachedMethod.find(method);
-        return cachedMethod == null ? null : findMethod(cachedMethod);
+        MetaMethod result = METHOD_CACHE.getAndPut(method, m -> {
+            CachedMethod cachedMethod = CachedMethod.find(m);
+            MetaMethod metaMethod = cachedMethod == null ? null : 
findMethod(cachedMethod);
+            if (metaMethod == null) metaMethod = NULL_METAMETHOD;
+            return metaMethod;
+        });
+        if (result == NULL_METAMETHOD) result = null;
+        return result;
     }
 
     /**

Reply via email to