jon         00/11/06 13:11:09

  Modified:    src/java/org/apache/velocity/runtime Runtime.java
  Log:
  create a parser cache for speed improvements (hopefully)
  
  this could have major MT sync issues. needs more testing. it *should* work since 
Stack extends Vector and Vector is syncronized.
  
  currently creates a hard coded # of 20 parsers...this should be changed
  to get its information from a properties file.
  
  Revision  Changes    Path
  1.33      +44 -7     
jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java
  
  Index: Runtime.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- Runtime.java      2000/11/06 04:14:14     1.32
  +++ Runtime.java      2000/11/06 21:11:06     1.33
  @@ -62,6 +62,7 @@
   import java.net.MalformedURLException;
   import java.util.Hashtable;
   import java.util.Properties;
  +import java.util.Stack;
   
   import org.apache.log.LogKit;
   import org.apache.log.Logger;
  @@ -144,7 +145,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Bowden</a>
  - * @version $Id: Runtime.java,v 1.32 2000/11/06 04:14:14 geirm Exp $
  + * @version $Id: Runtime.java,v 1.33 2000/11/06 21:11:06 jon Exp $
    */
   public class Runtime
   {
  @@ -205,8 +206,13 @@
         * The Runtime parser. This has to be changed to
         * a pool of parsers!
         */
  -    private static Parser parser;
  +    private static Stack parserStack;
       
  +    /**
  +      * Number of parsers to create
  +      */
  +    private static final int NUMBER_OF_PARSERS = 20;
  +    
       /** Indicate whether the Runtime has been fully initialized */
       private static boolean initialized;
       
  @@ -384,25 +390,56 @@
        * This still needs to be implemented.
        */
       private static void initializeParserPool()
  +    {
  +        parserStack = new Stack();
  +        for (int i=0;i<NUMBER_OF_PARSERS ;i++ )
  +        {
  +            parserStack.push (createNewParser());
  +        }
  +        Runtime.info ("Created: " + NUMBER_OF_PARSERS + " parsers.");
  +    }
  +
  +    /**
  +     * Returns a parser
  +     */
  +    public static Parser createNewParser()
       {
  -        // put this in a method and make a pool of
  -        // parsers.
  -        parser = new Parser();
  +        Parser parser = new Parser();
           Hashtable directives = new Hashtable();
           directives.put("foreach", new Foreach());
           directives.put("dummy", new Dummy());
           directives.put("include", new Include() );
           parser.setDirectives(directives);
  +        return parser;
       }
   
       /**
        * Parse the input stream and return the root of
        * AST node structure.
        */
  -    public synchronized static SimpleNode parse(InputStream inputStream)
  +    public static SimpleNode parse(InputStream inputStream)
           throws ParseException
       {
  -        return parser.parse(inputStream);
  +        Parser parser = null;
  +        try
  +        {
  +            parser = (Parser) parserStack.pop();
  +        }
  +        catch (Exception e)
  +        {
  +            parser = createNewParser();
  +        }
  +        SimpleNode sn = null;
  +        try
  +        {
  +            sn = parser.parse(inputStream);            
  +        }
  +        finally
  +        {
  +            if (parser != null)
  +                parserStack.push(parser);
  +        }
  +        return sn;
       }
       
       /**
  
  
  

Reply via email to