You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ta...@apache.org on 2026/07/24 13:25:16 UTC

(openwebbeans) branch startupperf created (now 26c2497af)

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

asf-gitbox-commits pushed a change to branch startupperf
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git


      at 26c2497af lazily create the repeatables Set (rarely used, saves an allocation per annotated element)

This branch includes the following new commits:

     new aabbaa621 resolve interceptable business methods via a Map lookup (O(n) instead of O(n^2)
     new 50fd394b9 speed up getNonPrivateMethods: drop O(n^2) List#contains, skip redundant per-level getMethods()
     new d4ab1dedb bucket getFilteredMethods override check by method name to avoid O(n^2) scan
     new 26c2497af lazily create the repeatables Set (rarely used, saves an allocation per annotated element)

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(openwebbeans) 03/04: bucket getFilteredMethods override check by method name to avoid O(n^2) scan

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch startupperf
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git

commit d4ab1dedb56f66a0da20c90b09437f0e8e90d868
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 15:22:24 2026 +0200

    bucket getFilteredMethods override check by method name to avoid O(n^2) scan
---
 .../webbeans/portable/AnnotatedElementFactory.java | 25 ++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedElementFactory.java b/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedElementFactory.java
index af02078b8..0788d7021 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedElementFactory.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedElementFactory.java
@@ -21,8 +21,12 @@ package org.apache.webbeans.portable;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -327,7 +331,7 @@ public final class AnnotatedElementFactory
         }
         methods = Collections.unmodifiableSet(getFilteredMethods(annotatedType.getJavaClass(),
                                                                  (Set)annotatedType.getMethods(),
-            new HashSet<>()));
+            new HashSet<>(), new HashMap<>()));
         Set<AnnotatedMethod<?>> old = annotatedMethodsOfTypeCache.putIfAbsent(annotatedType, methods);
         if (old != null)
         {
@@ -354,7 +358,8 @@ public final class AnnotatedElementFactory
         annotatedMethodsOfTypeCache.clear();
     }
     
-    private Set<? extends AnnotatedMethod<?>> getFilteredMethods(Class<?> type, Set<AnnotatedMethod<?>> allMethods, Set<AnnotatedMethod<?>> filteredMethods)
+    private Set<? extends AnnotatedMethod<?>> getFilteredMethods(Class<?> type, Set<AnnotatedMethod<?>> allMethods,
+            Set<AnnotatedMethod<?>> filteredMethods, Map<String, List<AnnotatedMethod<?>>> filteredByName)
     {
         if (type == null)
         {
@@ -362,17 +367,25 @@ public final class AnnotatedElementFactory
         }
         for (AnnotatedMethod<?> annotatedMethod: allMethods)
         {
-            if (annotatedMethod.getJavaMember().getDeclaringClass() == type && !isOverridden(annotatedMethod, filteredMethods))
+            if (annotatedMethod.getJavaMember().getDeclaringClass() == type && !isOverridden(annotatedMethod, filteredByName))
             {
                 filteredMethods.add(annotatedMethod);
+                filteredByName.computeIfAbsent(annotatedMethod.getJavaMember().getName(), k -> new ArrayList<>(2))
+                    .add(annotatedMethod);
             }
         }
-        return getFilteredMethods(type.getSuperclass(), allMethods, filteredMethods);
+        return getFilteredMethods(type.getSuperclass(), allMethods, filteredMethods, filteredByName);
     }
 
-    private boolean isOverridden(AnnotatedMethod<?> superclassMethod, Set<AnnotatedMethod<?>> methods)
+    private boolean isOverridden(AnnotatedMethod<?> superclassMethod, Map<String, List<AnnotatedMethod<?>>> filteredByName)
     {
-        for (AnnotatedMethod<?> subclassMethod : methods)
+        // only methods with the same name can override, so restrict the scan to that bucket (avoids O(n^2))
+        List<AnnotatedMethod<?>> sameName = filteredByName.get(superclassMethod.getJavaMember().getName());
+        if (sameName == null)
+        {
+            return false;
+        }
+        for (AnnotatedMethod<?> subclassMethod : sameName)
         {
             if (ClassUtil.isOverridden(subclassMethod.getJavaMember(), superclassMethod.getJavaMember()))
             {


(openwebbeans) 04/04: lazily create the repeatables Set (rarely used, saves an allocation per annotated element)

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch startupperf
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git

commit 26c2497afb7f0c2dbf5ef91888d3cb3815e516e8
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 15:22:32 2026 +0200

    lazily create the repeatables Set (rarely used, saves an allocation per annotated element)
---
 .../java/org/apache/webbeans/portable/AbstractAnnotated.java     | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java b/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java
index 9cdf2db3b..9141469d5 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java
@@ -56,7 +56,8 @@ public abstract class AbstractAnnotated implements Annotated
 
     /**Set of annotations*/
     private Set<Annotation> annotations = new HashSet<>();
-    private Set<Class<?>> repeatables = new HashSet<>();
+    /** lazily created - repeatable annotations are rare, so most annotated elements never need this Set */
+    private Set<Class<?>> repeatables;
 
     private final WebBeansContext webBeansContext;
     
@@ -124,6 +125,10 @@ public abstract class AbstractAnnotated implements Annotated
         }
         if (repeatables != null && !repeatables.isEmpty())
         {
+            if (this.repeatables == null)
+            {
+                this.repeatables = new HashSet<>();
+            }
             this.repeatables.addAll(repeatables.stream().map(Annotation::annotationType).collect(toList()));
             this.annotations.addAll(repeatables);
         }
@@ -131,7 +136,7 @@ public abstract class AbstractAnnotated implements Annotated
 
     public Set<Class<?>> getRepeatables()
     {
-        return repeatables;
+        return repeatables == null ? Collections.emptySet() : repeatables;
     }
 
     /**


(openwebbeans) 01/04: resolve interceptable business methods via a Map lookup (O(n) instead of O(n^2)

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch startupperf
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git

commit aabbaa621d4487776de31c20ab926c0391d7ab44
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 15:22:09 2026 +0200

    resolve interceptable business methods via a Map lookup (O(n) instead of O(n^2)
---
 .../intercept/InterceptorResolutionService.java    | 26 +++++++++++++---------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java b/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
index f944e93f4..450bd11d9 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
@@ -766,21 +766,27 @@ public class InterceptorResolutionService
                 }
             }
         }
+        // index the AnnotatedMethods by their java member so the matching below is O(n) instead of O(n^2).
+        // Each AnnotatedMethod wraps a distinct java.lang.reflect.Method, so the members are unique keys.
+        Map<Method, AnnotatedMethod<?>> annotatedMethodsByMember = new HashMap<>(annotatedMethods.size());
+        for (AnnotatedMethod<?> annotatedMethod : annotatedMethods)
+        {
+            annotatedMethodsByMember.put(annotatedMethod.getJavaMember(), annotatedMethod);
+        }
+
         for (Method interceptableMethod : interceptableMethods)
         {
-            for (AnnotatedMethod<?> annotatedMethod : annotatedMethods)
+            AnnotatedMethod<?> annotatedMethod = annotatedMethodsByMember.get(interceptableMethod);
+            if (annotatedMethod != null)
             {
-                if (annotatedMethod.getJavaMember().equals(interceptableMethod))
+                int modifiers = annotatedMethod.getJavaMember().getModifiers();
+                if (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers))
                 {
-                    int modifiers = annotatedMethod.getJavaMember().getModifiers();
-                    if (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers))
-                    {
-                        // we must only intercept business methods
-                        continue;
-                    }
-
-                    interceptableAnnotatedMethods.add(annotatedMethod);
+                    // we must only intercept business methods
+                    continue;
                 }
+
+                interceptableAnnotatedMethods.add(annotatedMethod);
             }
         }
 


(openwebbeans) 02/04: speed up getNonPrivateMethods: drop O(n^2) List#contains, skip redundant per-level getMethods()

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch startupperf
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git

commit 50fd394b94d96237b062e43617f1c1a548a88223
Author: tandraschko <ta...@apache.org>
AuthorDate: Fri Jul 24 15:22:16 2026 +0200

    speed up getNonPrivateMethods: drop O(n^2) List#contains, skip redundant per-level getMethods()
---
 .../java/org/apache/webbeans/util/ClassUtil.java   | 43 +++++++++++++++++-----
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java b/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
index 86d727c4d..80ffbe36f 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
@@ -293,22 +293,29 @@ public final class ClassUtil
     {
         Map<String, List<Method>> methodMap = new HashMap<>();
         List<Method> allMethods = new ArrayList<>(10);
+        // mirrors allMethods for O(1) membership checks (avoids O(n^2) List#contains with Method#equals)
+        Set<Method> allMethodsSet = new HashSet<>();
 
         Class<?> clazz = topClass;
 
         if (!clazz.isAnnotation() && clazz.isInterface())
         {
-            addNonPrivateMethods(topClass, excludeFinalMethods, methodMap, allMethods, clazz);
+            addNonPrivateMethods(topClass, excludeFinalMethods, methodMap, allMethods, allMethodsSet, clazz, true);
             for (Class<?> parent : clazz.getInterfaces())
             {
-                addNonPrivateMethods(topClass, excludeFinalMethods, methodMap, allMethods, parent);
+                addNonPrivateMethods(topClass, excludeFinalMethods, methodMap, allMethods, allMethodsSet, parent, true);
             }
         }
         else
         {
+            // topClass.getMethods() already returns every public method in the whole hierarchy, so only the
+            // top class needs that (expensive) call; superclasses only add their declared (non-public) methods.
+            // Public methods declared higher up are already collected and deduped via allMethodsSet.
+            boolean topLevel = true;
             while (clazz != null)
             {
-                addNonPrivateMethods(topClass, excludeFinalMethods, methodMap, allMethods, clazz);
+                addNonPrivateMethods(topClass, excludeFinalMethods, methodMap, allMethods, allMethodsSet, clazz, topLevel);
+                topLevel = false;
                 clazz = clazz.getSuperclass();
             }
         }
@@ -391,20 +398,36 @@ public final class ClassUtil
 
     private static void addNonPrivateMethods(Class<?> topClass, boolean excludeFinalMethods,
                                              Map<String, List<Method>> methodMap, List<Method> allMethods,
-                                             Class<?> clazz)
+                                             Set<Method> allMethodsSet, Class<?> clazz, boolean includeInheritedPublicMethods)
     {
-        List<Method> temp = new ArrayList<>(Arrays.asList(clazz.getMethods()));
-        for (Method method : clazz.getDeclaredMethods())
+        List<Method> temp;
+        if (includeInheritedPublicMethods)
         {
-            if (!temp.contains(method))
+            Method[] classMethods = clazz.getMethods();
+            Method[] declaredMethods = clazz.getDeclaredMethods();
+            temp = new ArrayList<>(classMethods.length + declaredMethods.length);
+            Collections.addAll(temp, classMethods);
+            for (Method method : declaredMethods)
             {
-                temp.add(method);
+                // a declared method is in getMethods() iff it is public, so only the non-public
+                // declared methods (private/protected/package) still need to be added here.
+                // This avoids both an O(n^2) List#contains and a temporary de-dup Set.
+                if (!Modifier.isPublic(method.getModifiers()))
+                {
+                    temp.add(method);
+                }
             }
         }
+        else
+        {
+            // inherited public methods were already collected from the top class (and deduped via
+            // allMethodsSet below), so this level only needs to contribute its declared methods.
+            temp = Arrays.asList(clazz.getDeclaredMethods());
+        }
 
         for (Method method : temp)
         {
-            if (allMethods.contains(method))
+            if (allMethodsSet.contains(method))
             {
                 continue;
             }
@@ -450,6 +473,7 @@ public final class ClassUtil
                 methods = new ArrayList<>();
                 methods.add(method);
                 allMethods.add(method);
+                allMethodsSet.add(method);
                 methodMap.put(method.getName(), methods);
             }
             else
@@ -459,6 +483,7 @@ public final class ClassUtil
                     // method is not overridden, so add it
                     methods.add(method);
                     allMethods.add(method);
+                    allMethodsSet.add(method);
                 }
             }
         }