ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [08/24] incubator-ignite git commit: # ignite-47 Store GridResourceField and GridResourceMethod to arrays instead of List (performance optimization) (cherry picked from commit 1107e4b)
Date Thu, 12 Mar 2015 06:44:46 GMT
# ignite-47 Store GridResourceField and GridResourceMethod to arrays instead of List (performance
optimization)
(cherry picked from commit 1107e4b)


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/573b658d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/573b658d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/573b658d

Branch: refs/heads/ignite-443
Commit: 573b658d598d588424fabc80578a4ae4325c9c34
Parents: 2d1b861
Author: sevdokimov <sevdokimov@gridgain.com>
Authored: Sat Mar 7 15:41:21 2015 +0300
Committer: sevdokimov <sevdokimov@gridgain.com>
Committed: Wed Mar 11 18:17:39 2015 +0300

----------------------------------------------------------------------
 .../processors/resource/GridResourceField.java  |  3 +
 .../processors/resource/GridResourceIoc.java    | 64 +++++++++++---------
 .../processors/resource/GridResourceMethod.java |  3 +
 .../resource/GridResourceProcessor.java         | 55 ++++-------------
 4 files changed, 54 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/573b658d/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceField.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceField.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceField.java
index b36978f..04f750f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceField.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceField.java
@@ -27,6 +27,9 @@ import java.lang.reflect.*;
  * Bean contains {@link Field} and {@link Annotation} for that class field.
  */
 class GridResourceField {
+    /** */
+    static final GridResourceField[] EMPTY_ARRAY = new GridResourceField[0];
+
     /** Field where resource should be injected. */
     private final Field field;
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/573b658d/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java
index 03d81f8..1ebe2f5 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceIoc.java
@@ -39,13 +39,11 @@ class GridResourceIoc {
         new ConcurrentHashMap8<>();
 
     /** Field cache. */
-    private final ConcurrentMap<Class<?>, ConcurrentMap<Class<? extends Annotation>,
-        List<GridResourceField>>> fieldCache =
+    private final ConcurrentMap<Class<?>, ConcurrentMap<Class<? extends Annotation>,
GridResourceField[]>> fieldCache =
         new ConcurrentHashMap8<>();
 
     /** Method cache. */
-    private final ConcurrentMap<Class<?>, ConcurrentMap<Class<? extends Annotation>,
-        List<GridResourceMethod>>> mtdCache =
+    private final ConcurrentMap<Class<?>, ConcurrentMap<Class<? extends Annotation>,
GridResourceMethod[]>> mtdCache =
         new ConcurrentHashMap8<>();
 
     /**
@@ -152,11 +150,9 @@ class GridResourceIoc {
             return false;
 
         // Check if already inspected to avoid indefinite recursion.
-        if (checkedObjs.contains(target))
+        if (!checkedObjs.add(target))
             return false;
 
-        checkedObjs.add(target);
-
         int annCnt = 0;
 
         boolean injected = false;
@@ -230,14 +226,14 @@ class GridResourceIoc {
         if (skipClss != null && skipClss.contains(targetCls))
             return false;
 
-        List<GridResourceField> fields = getFieldsWithAnnotation(dep, targetCls, annCls);
+        GridResourceField[] fields = getFieldsWithAnnotation(dep, targetCls, annCls);
 
-        if (!fields.isEmpty())
+        if (fields.length > 0)
             return true;
 
-        List<GridResourceMethod> mtds = getMethodsWithAnnotation(dep, targetCls, annCls);
+        GridResourceMethod[] mtds = getMethodsWithAnnotation(dep, targetCls, annCls);
 
-        if (mtds.isEmpty()) {
+        if (mtds.length == 0) {
             if (skipClss == null)
                 skipClss = F.addIfAbsent(skipCache, annCls, F.<Class<?>>newCSet());
 
@@ -323,22 +319,27 @@ class GridResourceIoc {
      * @param annCls Annotation.
      * @return Set of methods with given annotations.
      */
-    List<GridResourceMethod> getMethodsWithAnnotation(@Nullable GridDeployment dep,
Class<?> cls,
+    GridResourceMethod[] getMethodsWithAnnotation(@Nullable GridDeployment dep, Class<?>
cls,
         Class<? extends Annotation> annCls) {
-        List<GridResourceMethod> mtds = getMethodsFromCache(cls, annCls);
+        GridResourceMethod[] mtds = getMethodsFromCache(cls, annCls);
 
         if (mtds == null) {
-            mtds = new ArrayList<>();
+            List<GridResourceMethod> mtdsList = new ArrayList<>();
 
             for (Class cls0 = cls; !cls0.equals(Object.class); cls0 = cls0.getSuperclass())
{
                 for (Method mtd : cls0.getDeclaredMethods()) {
                     Annotation ann = mtd.getAnnotation(annCls);
 
                     if (ann != null)
-                        mtds.add(new GridResourceMethod(mtd, ann));
+                        mtdsList.add(new GridResourceMethod(mtd, ann));
                 }
             }
 
+            if (mtdsList.isEmpty())
+                mtds = GridResourceMethod.EMPTY_ARRAY;
+            else
+                mtds = mtdsList.toArray(new GridResourceMethod[mtdsList.size()]);
+
             cacheMethods(dep, cls, annCls, mtds);
         }
 
@@ -354,12 +355,12 @@ class GridResourceIoc {
      * @param annCls Annotation.
      * @return Set of entries with given annotations.
      */
-    private List<GridResourceField> getFieldsWithAnnotation(@Nullable GridDeployment
dep, Class<?> cls,
+    private GridResourceField[] getFieldsWithAnnotation(@Nullable GridDeployment dep, Class<?>
cls,
         Class<? extends Annotation> annCls) {
-        List<GridResourceField> fields = getFieldsFromCache(cls, annCls);
+        GridResourceField[] fields = getFieldsFromCache(cls, annCls);
 
         if (fields == null) {
-            fields = new ArrayList<>();
+            List<GridResourceField> fieldsList = new ArrayList<>();
 
             for (Class cls0 = cls; !cls0.equals(Object.class); cls0 = cls0.getSuperclass())
{
                 for (Field field : cls0.getDeclaredFields()) {
@@ -367,10 +368,15 @@ class GridResourceIoc {
 
                     if (ann != null || GridResourceUtils.mayRequireResources(field))
                         // Account for anonymous inner classes.
-                        fields.add(new GridResourceField(field, ann));
+                        fieldsList.add(new GridResourceField(field, ann));
                 }
             }
 
+            if (fieldsList.isEmpty())
+                fields = GridResourceField.EMPTY_ARRAY;
+            else
+                fields = fieldsList.toArray(new GridResourceField[fieldsList.size()]);
+
             cacheFields(dep, cls, annCls, fields);
         }
 
@@ -384,8 +390,8 @@ class GridResourceIoc {
      * @param annCls Annotation class for fields.
      * @return List of fields with given annotation, possibly {@code null}.
      */
-    @Nullable private List<GridResourceField> getFieldsFromCache(Class<?> cls,
Class<? extends Annotation> annCls) {
-        Map<Class<? extends Annotation>, List<GridResourceField>> annCache
= fieldCache.get(cls);
+    @Nullable private GridResourceField[] getFieldsFromCache(Class<?> cls, Class<?
extends Annotation> annCls) {
+        Map<Class<? extends Annotation>, GridResourceField[]> annCache = fieldCache.get(cls);
 
         return annCache != null ? annCache.get(annCls) : null;
     }
@@ -399,7 +405,7 @@ class GridResourceIoc {
      * @param fields Fields to cache.
      */
     private void cacheFields(@Nullable GridDeployment dep, Class<?> cls, Class<?
extends Annotation> annCls,
-        List<GridResourceField> fields) {
+        GridResourceField[] fields) {
         if (dep != null) {
             Set<Class<?>> classes = F.addIfAbsent(taskMap, dep.classLoader(),
F.<Class<?>>newCSet());
 
@@ -408,8 +414,8 @@ class GridResourceIoc {
             classes.add(cls);
         }
 
-        Map<Class<? extends Annotation>, List<GridResourceField>> rsrcFields
=
-            F.addIfAbsent(fieldCache, cls, F.<Class<? extends Annotation>, List<GridResourceField>>newCMap());
+        Map<Class<? extends Annotation>, GridResourceField[]> rsrcFields =
+            F.addIfAbsent(fieldCache, cls, F.<Class<? extends Annotation>, GridResourceField[]>newCMap());
 
         assert rsrcFields != null;
 
@@ -423,8 +429,8 @@ class GridResourceIoc {
      * @param annCls Annotation class for fields.
      * @return List of methods with given annotation, possibly {@code null}.
      */
-    @Nullable private List<GridResourceMethod> getMethodsFromCache(Class<?> cls,
Class<? extends Annotation> annCls) {
-        Map<Class<? extends Annotation>, List<GridResourceMethod>> annCache
= mtdCache.get(cls);
+    @Nullable private GridResourceMethod[] getMethodsFromCache(Class<?> cls, Class<?
extends Annotation> annCls) {
+        Map<Class<? extends Annotation>, GridResourceMethod[]> annCache = mtdCache.get(cls);
 
         return annCache != null ? annCache.get(annCls) : null;
     }
@@ -438,7 +444,7 @@ class GridResourceIoc {
      * @param mtds Methods to cache.
      */
     private void cacheMethods(@Nullable GridDeployment dep, Class<?> rsrcCls, Class<?
extends Annotation> annCls,
-        List<GridResourceMethod> mtds) {
+        GridResourceMethod[] mtds) {
         if (dep != null) {
             Set<Class<?>> classes = F.addIfAbsent(taskMap, dep.classLoader(),
F.<Class<?>>newCSet());
 
@@ -447,8 +453,8 @@ class GridResourceIoc {
             classes.add(rsrcCls);
         }
 
-        Map<Class<? extends Annotation>, List<GridResourceMethod>> rsrcMtds
= F.addIfAbsent(mtdCache,
-            rsrcCls, F.<Class<? extends Annotation>, List<GridResourceMethod>>newCMap());
+        Map<Class<? extends Annotation>, GridResourceMethod[]> rsrcMtds = F.addIfAbsent(mtdCache,
+            rsrcCls, F.<Class<? extends Annotation>, GridResourceMethod[]>newCMap());
 
         assert rsrcMtds != null;
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/573b658d/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceMethod.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceMethod.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceMethod.java
index 3853c95..aba9405 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceMethod.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceMethod.java
@@ -27,6 +27,9 @@ import java.lang.reflect.*;
  * Bean contains {@link Method} and {@link Annotation} for that method.
  */
 class GridResourceMethod {
+    /** */
+    static final GridResourceMethod[] EMPTY_ARRAY = new GridResourceMethod[0];
+
     /** Method which used to inject resource. */
     private final Method mtd;
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/573b658d/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java
index 8726536..f08a287 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/resource/GridResourceProcessor.java
@@ -141,19 +141,19 @@ public class GridResourceProcessor extends GridProcessorAdapter {
     public void invokeAnnotated(GridDeployment dep, Object target, Class<? extends Annotation>
annCls)
         throws IgniteCheckedException {
         if (target != null) {
-            Collection<Method> mtds = getMethodsWithAnnotation(dep, target.getClass(),
annCls);
-
-            if (mtds != null) {
-                for (Method mtd : mtds) {
-                    try {
-                        mtd.setAccessible(true);
-
-                        mtd.invoke(target);
-                    }
-                    catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException
e) {
-                        throw new IgniteCheckedException("Failed to invoke annotated method
[job=" + target + ", mtd=" + mtd +
-                            ", ann=" + annCls + ']', e);
-                    }
+            GridResourceMethod[] rsrcMtds = ioc.getMethodsWithAnnotation(dep, target.getClass(),
annCls);
+
+            for (GridResourceMethod rsrcMtd : rsrcMtds) {
+                Method mtd = rsrcMtd.getMethod();
+
+                try {
+                    mtd.setAccessible(true);
+
+                    mtd.invoke(target);
+                }
+                catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException
e) {
+                    throw new IgniteCheckedException("Failed to invoke annotated method [job="
+ target + ", mtd=" + mtd +
+                        ", ann=" + annCls + ']', e);
                 }
             }
         }
@@ -570,35 +570,6 @@ public class GridResourceProcessor extends GridProcessorAdapter {
     }
 
     /**
-     * Gets list of methods in specified class annotated with specified annotation.
-     *
-     * @param dep Class deployment.
-     * @param rsrcCls Class to find methods in.
-     * @param annCls Annotation to find annotated methods with.
-     * @return List of annotated methods.
-     */
-    @Nullable public Collection<Method> getMethodsWithAnnotation(GridDeployment dep,
Class<?> rsrcCls,
-        Class<? extends Annotation> annCls) {
-        assert dep != null;
-        assert rsrcCls != null;
-        assert annCls != null;
-
-        List<GridResourceMethod> mtds = ioc.getMethodsWithAnnotation(dep, rsrcCls,
annCls);
-
-        assert mtds != null;
-
-        if (!mtds.isEmpty()) {
-            return F.viewReadOnly(mtds, new C1<GridResourceMethod, Method>() {
-                @Override public Method apply(GridResourceMethod rsrcMtd) {
-                    return rsrcMtd.getMethod();
-                }
-            });
-        }
-
-        return null;
-    }
-
-    /**
      * Returns GridResourceIoc object. For tests only!!!
      *
      * @return GridResourceIoc object.


Mime
View raw message