velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1753343 - in /velocity/engine/trunk: src/changes/ velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/
Date Tue, 19 Jul 2016 07:45:13 GMT
Author: cbrisson
Date: Tue Jul 19 07:45:12 2016
New Revision: 1753343

URL: http://svn.apache.org/viewvc?rev=1753343&view=rev
Log:
[engine] applied patch from VELOCITY-815

Modified:
    velocity/engine/trunk/src/changes/changes.xml
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/MapGetExecutor.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java

Modified: velocity/engine/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/changes/changes.xml?rev=1753343&r1=1753342&r2=1753343&view=diff
==============================================================================
--- velocity/engine/trunk/src/changes/changes.xml (original)
+++ velocity/engine/trunk/src/changes/changes.xml Tue Jul 19 07:45:12 2016
@@ -27,6 +27,11 @@
   <body>
     <release version="2.0" date="In Subversion">
 
+      <action type="fix" dev="cbrisson" issue="VELOCITY-815" due-to="Oswaldo Hernandez">
+        Applied performance patch for MapGetExecutor: it's faster to directly use object
+        instance rather than to inspect all public interfaces
+      </action>
+      
       <action type="add" dev="cbrisson" issue="VELOCITY-799">
         The new configuration property context.autoreference.key, if present, allows to specify
the name
         of the reference under which the context is accessible in itself

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/MapGetExecutor.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/MapGetExecutor.java?rev=1753343&r1=1753342&r2=1753343&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/MapGetExecutor.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/MapGetExecutor.java
Tue Jul 19 07:45:12 2016
@@ -22,6 +22,7 @@ package org.apache.velocity.runtime.pars
 import org.apache.velocity.exception.VelocityException;
 import org.slf4j.Logger;
 
+import java.lang.reflect.Method;
 import java.util.Map;
 
 /**
@@ -36,48 +37,69 @@ public class MapGetExecutor
         extends AbstractExecutor 
 {
     private final String property;
+    private final boolean isAlive;
 
-    public MapGetExecutor(final Logger log, final Class clazz, final String property)
+    public MapGetExecutor(final Logger log, final Object object, final String property)
     {
         this.log = log;
         this.property = property;
-        discover(clazz);
+        isAlive = discover(object);
     }
 
-    protected void discover (final Class clazz)
+    @Override
+    public Method getMethod()
     {
-        Class [] interfaces = clazz.getInterfaces();
-        for (int i = 0 ; i < interfaces.length; i++)
+        if (isAlive())
         {
-            if (interfaces[i].equals(Map.class))
+            return MapGetMethod.instance();
+        }
+        return null;
+    }
+
+    @Override
+    public boolean isAlive()
+    {
+        return isAlive;
+    }
+
+    protected boolean discover (final Object object)
+    {
+        if (object instanceof Map)
+        {
+            if (property != null)
             {
-                try
-                {
-                    if (property != null)
-                    {
-                        setMethod(Map.class.getMethod("get", new Class [] { Object.class
}));
-                    }
-                }
-                /**
-                 * pass through application level runtime exceptions
-                 */
-                catch( RuntimeException e )
-                {
-                    throw e;
-                }
-                catch(Exception e)
-                {
-                    String msg = "Exception while looking for get('" + property + "') method";
-                    log.error(msg, e);
-                    throw new VelocityException(msg, e);
-                }
-                break;
+                return true;
             }
         }
+        return false;
     }
 
     public Object execute(final Object o)
     {
         return ((Map) o).get(property);
+    }
+
+    private static final class MapGetMethod
+    {
+        private static final Method instance;
+
+        static
+        {
+            try
+            {
+                instance = Map.class.getMethod("get", new Class[]{Object.class});
+            }
+            catch (final NoSuchMethodException mapGetMethodMissingError)
+            {
+                throw new Error(mapGetMethodMissingError);
+            }
+        }
+
+        private MapGetMethod() { }
+
+        static Method instance()
+        {
+            return instance;
+        }
     } 
 }

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java?rev=1753343&r1=1753342&r2=1753343&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
Tue Jul 19 07:45:12 2016
@@ -244,7 +244,7 @@ public class UberspectImpl implements Ub
          */
         if (!executor.isAlive()) 
         {
-            executor = new MapGetExecutor(log, claz, identifier);
+            executor = new MapGetExecutor(log, obj, identifier);
         }
 
         /*



Mime
View raw message