velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r679920 - in /velocity/engine/trunk/src/java/org/apache/velocity/runtime: VelocimacroFactory.java directive/RuntimeMacro.java
Date Fri, 25 Jul 2008 22:56:14 GMT
Author: nbubna
Date: Fri Jul 25 15:56:14 2008
New Revision: 679920

URL: http://svn.apache.org/viewvc?rev=679920&view=rev
Log:
VELOCITY-607 use more fine-grained synchronization in to reduce needless thread blocking (and
a few other minor perf tweaks)

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/VelocimacroFactory.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/VelocimacroFactory.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/VelocimacroFactory.java?rev=679920&r1=679919&r2=679920&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/VelocimacroFactory.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/VelocimacroFactory.java Fri
Jul 25 15:56:14 2008
@@ -484,22 +484,19 @@
      */
     public Directive getVelocimacro(String vmName, String sourceTemplate)
     {
-        VelocimacroProxy vp = null;
-
-        synchronized(this)
+        VelocimacroProxy vp = vmManager.get(vmName, sourceTemplate);
+        if (vp == null)
         {
-            /*
-             *  don't ask - do
-             */
-
-            vp = vmManager.get(vmName, sourceTemplate);
+            return null;
+        }
 
+        synchronized(vp)
+        {
             /*
              *  if this exists, and autoload is on, we need to check
              *  where this VM came from
              */
-
-            if (vp != null && getAutoload())
+            if (getAutoload())
             {
                 /*
                  *  see if this VM came from a library.  Need to pass sourceTemplate

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=679920&r1=679919&r2=679920&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
Fri Jul 25 15:56:14 2008
@@ -19,6 +19,7 @@
  * under the License.
  */
 
+import org.apache.commons.lang.text.StrBuilder;
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.runtime.parser.node.Node;
 import org.apache.velocity.runtime.parser.Token;
@@ -124,26 +125,35 @@
         this.context = context;
         this.node = node;
 
-        StringBuffer buffer = new StringBuffer();
         Token t = node.getFirstToken();
 
-        /**
-         * Retrieve the literal text
-         */
-        while (t != null && t != node.getLastToken())
+        if (t == node.getLastToken())
         {
-            buffer.append(t.image);
-            t = t.next;
+            literal = t.image;
         }
-
-        if (t != null)
+        else
         {
-            buffer.append(t.image);
+            // guessing that most macros are much longer than
+            // the 32 char default capacity.  let's guess 4x bigger :)
+            StrBuilder text = new StrBuilder(128);
+            /**
+             * Retrieve the literal text
+             */
+            while (t != null && t != node.getLastToken())
+            {
+                text.append(t.image);
+                t = t.next;
+            }
+            if (t != null)
+            {
+                text.append(t.image);
+            }
+
+            /**
+             * Store the literal text
+             */
+            literal = text.toString();
         }
-        /**
-         * Store the literal text
-         */
-        literal = buffer.toString();
     }
 
     /**
@@ -174,8 +184,7 @@
         /**
          * first look in the source template
          */
-        Object o = rsvc.getVelocimacro(macroName,
-                sourceTemplate);
+        Object o = rsvc.getVelocimacro(macroName, sourceTemplate);
         if (o != null && o instanceof VelocimacroProxy)
         {
             vmProxy = (VelocimacroProxy)o;
@@ -213,9 +222,13 @@
              */
             try
             {
-                synchronized(this)
+                synchronized (vmProxy)
                 {
                     vmProxy.init(rsvc, this.context, this.node);
+                    /**
+                     * render it
+                     */
+                    return vmProxy.render(context, writer, node);
                 }
             }
             catch (TemplateInitException die)
@@ -224,10 +237,6 @@
 
                 throw new ParseErrorException(die.getMessage(), info);
             }
-            /**
-             * render it
-             */
-            return vmProxy.render(context, writer, node);
         }
 
         /**



Mime
View raw message