[I am resending this patch, since my original posting apparently did not
make it to the list.]

Attached is a patch implementing tag pooling and tag reuse for Jasper 2.
I am also attaching a new class org.apache.jasper.runtime.TagHandlerPool
implementing a tag handler pool (org.apache.jasper.util.SimplePool
is not being leveraged, since we want our pool's get() method to
encapsulate the logic of instantiating a new tag handler if the pool
is empty, and its reuse() method to encapsulate the logic of calling
the handler's release() method if the pool is full).


Tag pooling/reuse is implemented as follows:

- Every custom tag is assigned a tag handler pool, based on its full tag name
and attribute set.

- Tag handler pools are declared as servlet instance variables, so
that they can be shared across page invocations.

- Tag handler pools are instantiated with a
yet-to-be-made-configurable capacity at servlet construction time
(current default capacity is 5).

- When a tag handler is required, it is retrieved from the appropriate
tag handler pool. If the pool is empty, a new tag handler is instantiated and
returned.

- When a tag handler is no longer needed, it is returned to the appropriate
pool. It is added to the pool, unless the pool has already reached its
capacity, in which case the tag handler's release() method is called.

- The servlet's destroy() method enumerates the servlet's tag handler
pools and calls the release() method of every available tag handler.


Outstanding issues:
- Make tag handler pool capacity configurable.
- Optimize tag reuse in the body of iteration tags.


Thanks,


Jan
Executing ssh-askpass to query the password...
Warning: Remote host denied X11 forwarding, perhaps xauth program could not be run on 
the server side.
cvs server: Diffing src
cvs server: Diffing src/bin
cvs server: Diffing src/share
cvs server: Diffing src/share/org
cvs server: Diffing src/share/org/apache
cvs server: Diffing src/share/org/apache/jasper
cvs server: Diffing src/share/org/apache/jasper/compiler
Index: src/share/org/apache/jasper/compiler/Generator.java
===================================================================
RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v
retrieving revision 1.17
diff -u -r1.17 Generator.java
--- src/share/org/apache/jasper/compiler/Generator.java 5 Jun 2002 22:01:33 -0000      
 1.17
+++ src/share/org/apache/jasper/compiler/Generator.java 6 Jun 2002 19:12:37 -0000
@@ -96,6 +96,7 @@
     private boolean breakAtLF;
     private PageInfo pageInfo;
     private int maxTagNesting;
+    private Vector tagHandlerPoolNames;
 
     /**
      * @param s the input string
@@ -158,6 +159,58 @@
     }
 
     /**
+     * Compiles list of tag handler pool names.
+     */
+    private void compileTagHandlerPoolList(Node.Nodes page)
+           throws JasperException {
+
+       class TagHandlerPoolVisitor extends Node.Visitor {
+
+           private Vector names;
+
+           /*
+            * Constructor
+            *
+            * @param v Vector of tag handler pool names to populate
+            */
+           TagHandlerPoolVisitor(Vector v) {
+               names = v;
+           }
+
+           /*
+            * Gets the name of the tag handler pool for the given custom tag
+            * and adds it to the list of tag handler pool names unless it is
+            * already contained in it.
+            */
+           public void visit(Node.CustomTag n) throws JasperException {
+               String name = n.getTagHandlerPoolName();
+               if (!names.contains(name)) {
+                   names.add(name);
+               }
+               visitBody(n);
+           }
+       }
+
+       page.visit(new TagHandlerPoolVisitor(tagHandlerPoolNames));
+    }
+
+    /**
+     * Generates the destroy() method which is responsible for calling the
+     * release() method on every tag handler in any of the tag handler pools.
+     */
+    private void generateDestroy() {
+       out.printil("public void jspDestroy() {");
+       out.pushIndent();
+       for (int i=0; i<tagHandlerPoolNames.size(); i++) {
+           out.printin((String) tagHandlerPoolNames.elementAt(i));
+           out.println(".release();");
+       }
+       out.popIndent();        
+       out.printil("}");
+       out.println();
+    }
+
+    /**
      * Generates the beginning of the static portion of the servelet.
      */
     private void generatePreamble(Node.Nodes page) throws JasperException {
@@ -204,15 +257,13 @@
 
         // Static data for getIncludes()
         out.printil("private static java.util.Vector _jspx_includes;");
-        out.println();  
         out.println();
         List includes = pageInfo.getIncludes();
         iter = includes.iterator();
         if( !includes.isEmpty() ) {
             out.printil("static {");
             out.pushIndent();
-            out.printin(
-                "_jspx_includes = new java.util.Vector(");
+            out.printin("_jspx_includes = new java.util.Vector(");
             out.print(""+includes.size());
             out.println(");");
             while (iter.hasNext()) {
@@ -223,15 +274,27 @@
             out.popIndent();                     
             out.printil("}");
             out.println();  
-            out.println();
         }
 
-       // Class fields declarations
+       // Class variable declarations
        
-       // Constructor (empty so far) here
+       // Instance variable declarations
+       if (!tagHandlerPoolNames.isEmpty()) {
+           for (int i=0; i<tagHandlerPoolNames.size(); i++) {
+               out.printil("private org.apache.jasper.runtime.TagHandlerPool "
+                           + tagHandlerPoolNames.elementAt(i) + ";");
+           }
+            out.println();
+       }
 
-       // Methods here
+       // Constructor
+       if (!tagHandlerPoolNames.isEmpty()) {
+           generateServletConstructor(servletClassName);
+       }
 
+       /*
+        * Methods here
+        */
        // Method used to get compile time include file dependencies
         out.printil("public java.util.List getIncludes() {");
         out.pushIndent();
@@ -239,7 +302,11 @@
         out.popIndent();
         out.printil("}");
         out.println();
-        out.println();
+
+       // destroy() method
+       if (!tagHandlerPoolNames.isEmpty()) {
+           generateDestroy();
+       }
 
        // Now the service method
        out.printin("public void ");
@@ -298,6 +365,21 @@
        out.println();
     }
 
+    /*
+     * Generates the servlet constructor.
+     */
+    private void generateServletConstructor(String servletClassName) {
+       out.printil("public " + servletClassName + "() {");
+       out.pushIndent();
+       for (int i=0; i<tagHandlerPoolNames.size(); i++) {
+           out.printin((String) tagHandlerPoolNames.elementAt(i));
+           out.println(" = new org.apache.jasper.runtime.TagHandlerPool();");
+       }
+       out.popIndent();
+       out.printil("}");
+       out.println();
+    }
+
     /**
      * Generate codes defining the classes used in the servlet.
      * 1. Servlet state object, used to pass servlet info round methods.
@@ -1081,9 +1163,14 @@
            out.printin(tagHandlerClass.getName());
            out.print(" ");
            out.print(tagHandlerVar);
-           out.print(" = new ");
+           out.print(" = (");
+           out.print(tagHandlerClass.getName());
+           out.print(") ");
+           out.print(n.getTagHandlerPoolName());
+           out.print(".get(");
            out.print(tagHandlerClass.getName());
-           out.println("();");
+           out.println(".class);");
+
            generateSetters(n, tagHandlerVar, handlerInfo);
            
             if (implementsTryCatchFinally) {
@@ -1206,13 +1293,17 @@
                 out.pushIndent();
                out.printin(tagHandlerVar);
                out.println(".doFinally();");
-                out.printin(tagHandlerVar);
-                out.println(".release();");
+               out.printin(n.getTagHandlerPoolName());
+               out.print(".reuse(");
+               out.print(tagHandlerVar);
+               out.println(");");
                 out.popIndent();
                 out.printil("}");
             } else {
-                out.printin(tagHandlerVar);
-                out.println(".release();");
+               out.printin(n.getTagHandlerPoolName());
+               out.print(".reuse(");
+               out.print(tagHandlerVar);
+               out.println(");");
            }
 
            // Declare and synchronize AT_END variables
@@ -1266,28 +1357,6 @@
            }
        }
 
-       private String replace(String name, char replace, String with) {
-           StringBuffer buf = new StringBuffer();
-           int begin = 0;
-           int end;
-           int last = name.length();
-
-           while (true) {
-               end = name.indexOf(replace, begin);
-               if (end < 0) {
-                   end = last;
-               }
-               buf.append(name.substring(begin, end));
-               if (end == last) {
-                   break;
-               }
-               buf.append(with);
-               begin = end + 1;
-           }
-
-           return buf.toString();
-       }
-       
        /*
         * Creates a tag variable name by concatenating the given prefix and
         * shortName and replacing '-' with "$1", '.' with "$2", and ':' with
@@ -1296,16 +1365,16 @@
        private String createTagVarName(String fullName, String prefix,
                                        String shortName) {
            if (prefix.indexOf('-') >= 0)
-               prefix = replace(prefix, '-', "$1");
+               prefix = JspUtil.replace(prefix, '-', "$1");
            if (prefix.indexOf('.') >= 0)
-               prefix = replace(prefix, '.', "$2");
+               prefix = JspUtil.replace(prefix, '.', "$2");
 
            if (shortName.indexOf('-') >= 0)
-               shortName = replace(shortName, '-', "$1");
+               shortName = JspUtil.replace(shortName, '-', "$1");
            if (shortName.indexOf('.') >= 0)
-               shortName = replace(shortName, '.', "$2");
+               shortName = JspUtil.replace(shortName, '.', "$2");
            if (shortName.indexOf(':') >= 0)
-               shortName = replace(shortName, ':', "$3");
+               shortName = JspUtil.replace(shortName, ':', "$3");
 
            synchronized (tagVarNumbers) {
                String varName = prefix + "_" + shortName + "_";
@@ -1493,6 +1562,7 @@
        pageInfo = compiler.getPageInfo();
        beanInfo = pageInfo.getBeanRepository();
        breakAtLF = ctxt.getOptions().getMappedFile();
+       tagHandlerPoolNames = new Vector();
     }
 
     /**
@@ -1505,6 +1575,7 @@
                                Node.Nodes page) throws JasperException {
        Generator gen = new Generator(out, compiler);
 
+       gen.compileTagHandlerPoolList(page);
        gen.generatePreamble(page);
        page.visit(gen.new GenerateVisitor(out, gen.methodsBuffer));
        gen.generatePostamble(page);
Index: src/share/org/apache/jasper/compiler/JspUtil.java
===================================================================
RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/JspUtil.java,v
retrieving revision 1.3
diff -u -r1.3 JspUtil.java
--- src/share/org/apache/jasper/compiler/JspUtil.java   20 May 2002 18:02:56 -0000     
 1.3
+++ src/share/org/apache/jasper/compiler/JspUtil.java   6 Jun 2002 19:12:37 -0000
@@ -356,7 +356,7 @@
        }
        return escString;
     }
-    
+
     /**
      *  Escape the 5 entities defined by XML.
      */
@@ -380,6 +380,32 @@
             }
         }
         return sb.toString();
+    }
+
+    /**
+     * Replaces any occurrence of <tt>replace</tt> with <tt>with</tt> in the
+     * given string.
+     */
+    public static String replace(String name, char replace, String with) {
+       StringBuffer buf = new StringBuffer();
+       int begin = 0;
+       int end;
+       int last = name.length();
+
+       while (true) {
+           end = name.indexOf(replace, begin);
+           if (end < 0) {
+               end = last;
+           }
+           buf.append(name.substring(begin, end));
+           if (end == last) {
+               break;
+           }
+           buf.append(with);
+           begin = end + 1;
+       }
+       
+       return buf.toString();
     }
     
     public static class ValidAttribute {
Index: src/share/org/apache/jasper/compiler/Node.java
===================================================================
RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v
retrieving revision 1.11
diff -u -r1.11 Node.java
--- src/share/org/apache/jasper/compiler/Node.java      5 Jun 2002 23:29:53 -0000      
 1.11
+++ src/share/org/apache/jasper/compiler/Node.java      6 Jun 2002 19:12:38 -0000
@@ -75,6 +75,8 @@
  */
 
 public abstract class Node {
+
+    private static final String TAG_POOL_PREFIX = "_jspx_tagPool_";
     
     protected Attributes attrs;
     protected Nodes body;
@@ -669,6 +671,7 @@
        private boolean hasUsebean;
        private boolean hasIncludeAction;
        private boolean hasSetProperty;
+       private String tagHandlerPoolName;
 
        public CustomTag(Attributes attrs, Mark start, String name,
                         String prefix, String shortName, Node parent) {
@@ -676,6 +679,9 @@
            this.name = name;
            this.prefix = prefix;
            this.shortName = shortName;
+           this.tagHandlerPoolName = createTagHandlerPoolName(prefix,
+                                                              shortName,
+                                                              attrs);
        }
 
        public void accept(Visitor v) throws JasperException {
@@ -749,6 +755,55 @@
 
        public boolean isHasSetProperty() {
            return hasSetProperty;
+       }
+
+       /**
+        * Gets the name of the tag handler pool whose tag handlers may be
+        * (re)used to service this action.
+        * (This information is used by the compiler's code generator.)
+        *
+        * @return The name of the tag handler pool
+        */
+       public String getTagHandlerPoolName() {
+           return tagHandlerPoolName;
+       }
+
+       /*
+        * Creates the name of the tag handler pool whose tag handlers may be
+        * (re)used to service this action.
+        *
+        * @return The name of the tag handler pool
+        */
+       private String createTagHandlerPoolName(String prefix,
+                                               String shortName,
+                                               Attributes attrs) {
+           String poolName = null;
+
+           if (prefix.indexOf('-') >= 0)
+               prefix = JspUtil.replace(prefix, '-', "$1");
+           if (prefix.indexOf('.') >= 0)
+               prefix = JspUtil.replace(prefix, '.', "$2");
+
+           if (shortName.indexOf('-') >= 0)
+               shortName = JspUtil.replace(shortName, '-', "$1");
+           if (shortName.indexOf('.') >= 0)
+               shortName = JspUtil.replace(shortName, '.', "$2");
+           if (shortName.indexOf(':') >= 0)
+               shortName = JspUtil.replace(shortName, ':', "$3");
+
+           poolName = TAG_POOL_PREFIX + prefix + "_" + shortName;
+           if (attrs != null) {
+               String[] attrNames = new String[attrs.getLength()];
+               for (int i=0; i<attrNames.length; i++) {
+                   attrNames[i] = attrs.getQName(i);
+               }
+               Arrays.sort(attrNames, Collections.reverseOrder());
+               for (int i=0; i<attrNames.length; i++) {
+                   poolName = poolName + "_" + attrNames[i];
+               }
+           }
+
+           return poolName;
        }
     }
 
cvs server: Diffing src/share/org/apache/jasper/core
cvs server: Diffing src/share/org/apache/jasper/logging
cvs server: Diffing src/share/org/apache/jasper/resources
cvs server: Diffing src/share/org/apache/jasper/runtime
cvs server: src/share/org/apache/jasper/runtime/TagHandlerPool.java is a new entry, no 
comparison available
cvs server: Diffing src/share/org/apache/jasper/servlet
cvs server: Diffing src/share/org/apache/jasper/util
cvs server: Diffing src/share/org/apache/jasper/xmlparser
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights 
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:  
 *       "This product includes software developed by the 
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written 
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */ 

package org.apache.jasper.runtime;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

/**
 * Pool of tag handlers that can be reused.
 *
 * @author Jan Luehe
 */
public class TagHandlerPool {
    
    private static final int MAX_POOL_SIZE = 5;

    private Tag[] handlers;

    // index of next available tag handler
    private int current;

    /**
     * Constructs a tag handler pool with the default capacity.
     */
    public TagHandlerPool() {
        this(MAX_POOL_SIZE);
    }

    /**
     * Constructs a tag handler pool with the given capacity.
     *
     * @param capacity Tag handler pool capacity
     */
    public TagHandlerPool(int capacity) {
        this.handlers = new Tag[capacity];
        this.current = -1;
    }

    /**
     * Gets the next available tag handler from this tag handler pool,
     * instantiating one if this tag handler pool is empty.
     *
     * @param handlerClass Tag handler class
     *
     * @return Reused or newly instantiated tag handler
     *
     * @throws JspException if a tag handler cannot be instantiated
     */
    public synchronized Tag get(Class handlerClass) throws JspException {
        Tag handler = null;

        if (current >= 0) {
            handler = handlers[current--];
        } else {
            try {
                return (Tag) handlerClass.newInstance();
            } catch (Exception e) {
                throw new JspException(e.getMessage(), e);
            }
        }

        return handler;
    }

    /**
     * Adds the given tag handler to this tag handler pool, unless this tag
     * handler pool has already reached its capacity, in which case the tag
     * handler's release() method is called.
     *
     * @param handler Tag handler to add to this tag handler pool
     */
    public synchronized void reuse(Tag handler) {
        if (current < (handlers.length - 1))
            handlers[++current] = handler;
        else
            handler.release();
    }

    /**
     * Calls the release() method of all available tag handlers in this tag
     * handler pool.
     */
    public synchronized void release() {
        for (int i=current; i>=0; i--) {
            handlers[i].release();
        }
    }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to