You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Juozas Baliuka <ba...@mail.lt> on 2002/08/26 19:47:14 UTC

[PATCH] optimization

Index: Parser.jjt
===================================================================
RCS file:
/home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/runtime/parser
/Parser.jjt,v
retrieving revision 1.75
diff -u -r1.75 Parser.jjt
--- Parser.jjt 27 Apr 2002 19:32:39 -0000 1.75
+++ Parser.jjt 26 Aug 2002 17:45:35 -0000
@@ -142,7 +142,7 @@
     /**
      *  This Hashtable contains a list of all of the dynamic directives.
      */
-    private Hashtable directives = new Hashtable(0);
+    private Hashtable directives = new Hashtable(0);//Is it for "old"
java.util.* ?

     /**
      *  Name of current template we are parsing.  Passed to us in parse()
@@ -285,17 +285,19 @@

         String strDirective = strImage.substring(iLast + 1);

+        String directive = strDirective.substring(1);
+
         boolean bRecognizedDirective = false;

         /*
          *  is this a PD or a control directive?
          */

-        if ( isDirective( strDirective.substring(1)))
+        if ( isDirective( directive ))
         {
            bRecognizedDirective = true;
         }
-        else if ( rsvc.isVelocimacro( strDirective.substring(1),
currentTemplateName))
+        else if ( rsvc.isVelocimacro( directive , currentTemplateName))
         {
             bRecognizedDirective = true;
         }
@@ -303,12 +305,12 @@
         {
             /* order for speed? */

-            if ( strDirective.substring(1).equals("if")
-                || strDirective.substring(1).equals("end")
-                || strDirective.substring(1).equals("set")
-                || strDirective.substring(1).equals("else")
-                || strDirective.substring(1).equals("elseif")
-                || strDirective.substring(1).equals("stop")
+            if ( directive.equals("if")
+                || directive.equals("end")
+                || directive.equals("set")
+                || directive.equals("else")
+                || directive.equals("elseif")
+                || directive.equals("stop")
             )
             {
                 bRecognizedDirective = true;



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] optimization

Posted by Juozas Baliuka <ba...@mail.lt>.
"stateStackPush/stateStackPop" are used almost for all "actions"
...........................................................................
        lparen = ( (Integer) h.get("lparen")).intValue();
        rparen = ( (Integer) h.get("rparen")).intValue();

        SwitchTo( ( (Integer) h.get("lexstate")).intValue() );
...................................................................

it is possible to optimize Hastable stuff  with some trivial class:

 private static class State{
           int lparen ;
           int rparen ;
           int lexstate;
     public String toString(){ //for debug }
 }
 
 ..........................................
         lparen = state.lparen;
         rparen = state.rparen;
 ..........................................

    or :
   -  h = (Hashtable) stateStack.pop();
   + this.currentState = (State) stateStack.pop();





<snip>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Compiler was Re: [PATCH] optimization

Posted by Juozas Baliuka <ba...@mail.lt>.
Agree,
but may be it was some motivation to add Compiler class and to have it on
"TODO". ?


>
> ----- Original Message -----
> From: "Juozas Baliuka" <ba...@mail.lt>
> To: "Velocity Developers List" <ve...@jakarta.apache.org>
> Sent: Tuesday, August 27, 2002 10:15 PM
> Subject: Compiler was Re: [PATCH] optimization
>
>
> > Hi,
> > I see some incomplete compiler.
> > Doe's somebody works on Compiler (  Tree -> byte code ) ?
>
> I guess it was discussed earlier here (or maybe it was on FreeMarker list?
I
> don't remember, however the reasoning stands for both projects
> nevertheless...)
>
> Basically, the only thing you could optimize would be tree traversal -
> instead of walking the AST tree on every evaluation and calling the
> appropriate method on every node, you could generate the bytecode that
would
> essentially be the sequence of these same method calls on every node in
the
> preorder traversal. That would buy you minimal speed improvement, so it's
> just not worth the effort. If you *really* want templates compiled to
> bytecode, check out the Tea template engine - it always compiles templates
> to bytecode (as a consequence, the Tea template language is strongly
typed,
> which is an unusual feature in template engines...)
>
> Attila.
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Compiler was Re: [PATCH] optimization

Posted by Attila Szegedi <sz...@freemail.hu>.
----- Original Message -----
From: "Juozas Baliuka" <ba...@mail.lt>
To: "Velocity Developers List" <ve...@jakarta.apache.org>
Sent: Tuesday, August 27, 2002 10:15 PM
Subject: Compiler was Re: [PATCH] optimization


> Hi,
> I see some incomplete compiler.
> Doe's somebody works on Compiler (  Tree -> byte code ) ?

I guess it was discussed earlier here (or maybe it was on FreeMarker list? I
don't remember, however the reasoning stands for both projects
nevertheless...)

Basically, the only thing you could optimize would be tree traversal -
instead of walking the AST tree on every evaluation and calling the
appropriate method on every node, you could generate the bytecode that would
essentially be the sequence of these same method calls on every node in the
preorder traversal. That would buy you minimal speed improvement, so it's
just not worth the effort. If you *really* want templates compiled to
bytecode, check out the Tea template engine - it always compiles templates
to bytecode (as a consequence, the Tea template language is strongly typed,
which is an unusual feature in template engines...)

Attila.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Compiler was Re: [PATCH] optimization

Posted by Juozas Baliuka <ba...@mail.lt>.
I do not believe compiled tree can be more "fast", it can be more problems
with
compiled templates "InternalError" :).
Possible way to test/debug  is to use
Tree ->JAVA source ->javac->byte code
If it becomes "better" compile without javac and use compiled tree by javac
for debuging ( or forget this idea if no good results).
I believe it is better to "cache" some actions after traversing (like it
doe's with reflect stuff).
generate output and optimized actions at first time:
visit->
---visit->
-------visit->
-------visit->.....
..........................................
next time :
execute
execute
execute
execute
................................
It has meaning for some templates, but I am not sure at this time.
I will try to find some possible ways.

> On 8/27/02 4:15 PM, "Juozas Baliuka" <ba...@mail.lt> wrote:
>
> > Hi,
> > I see some incomplete compiler.
> > Doe's somebody works on Compiler (  Tree -> byte code ) ?
> > Is it some ideas about compiling and debuging compiled templates ?
> >
> > Attached some minor optimization for parser.
>
> Thx.  I'll take a look at the diffs.
>
> Re the compiler, this is a subject of discussion from time to time.
>
> My belief is that compilation won't buy us much after a template is parsed
> into the AST - because rendering the template (running the AST) is just
> method calls - no parsing...
>
> --
> Geir Magnusson Jr.
> Research & Development, Adeptra Inc.
> geirm@adeptra.com
> +1-203-247-1713
>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Compiler was Re: [PATCH] optimization

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 8/27/02 4:15 PM, "Juozas Baliuka" <ba...@mail.lt> wrote:

> Hi,
> I see some incomplete compiler.
> Doe's somebody works on Compiler (  Tree -> byte code ) ?
> Is it some ideas about compiling and debuging compiled templates ?
> 
> Attached some minor optimization for parser.

Thx.  I'll take a look at the diffs.

Re the compiler, this is a subject of discussion from time to time.

My belief is that compilation won't buy us much after a template is parsed
into the AST - because rendering the template (running the AST) is just
method calls - no parsing...

-- 
Geir Magnusson Jr. 
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Compiler was Re: [PATCH] optimization

Posted by Juozas Baliuka <ba...@mail.lt>.
Hi,
I see some incomplete compiler.
Doe's somebody works on Compiler (  Tree -> byte code ) ?
Is it some ideas about compiling and debuging compiled templates ?

Attached some minor optimization for parser.
<snip>

Re: [PATCH] optimization

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 8/26/02 1:47 PM, "Juozas Baliuka" <ba...@mail.lt> wrote:

> Index: Parser.jjt
> ===================================================================
> RCS file:
> /home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/runtime/parser
> /Parser.jjt,v
> retrieving revision 1.75
> diff -u -r1.75 Parser.jjt
> --- Parser.jjt 27 Apr 2002 19:32:39 -0000 1.75
> +++ Parser.jjt 26 Aug 2002 17:45:35 -0000
> @@ -142,7 +142,7 @@
>    /**
>     *  This Hashtable contains a list of all of the dynamic directives.
>     */
> -    private Hashtable directives = new Hashtable(0);
> +    private Hashtable directives = new Hashtable(0);//Is it for "old"
> java.util.* ?
> 
>    /**
>     *  Name of current template we are parsing.  Passed to us in parse()
> @@ -285,17 +285,19 @@
> 
>        String strDirective = strImage.substring(iLast + 1);
> 
> +        String directive = strDirective.substring(1);
> +
>        boolean bRecognizedDirective = false;
> 
>        /*
>         *  is this a PD or a control directive?
>         */
> 
> -        if ( isDirective( strDirective.substring(1)))
> +        if ( isDirective( directive ))
>        {
>           bRecognizedDirective = true;
>        }
> -        else if ( rsvc.isVelocimacro( strDirective.substring(1),
> currentTemplateName))
> +        else if ( rsvc.isVelocimacro( directive , currentTemplateName))
>        {
>            bRecognizedDirective = true;
>        }
> @@ -303,12 +305,12 @@
>        {
>            /* order for speed? */
> 
> -            if ( strDirective.substring(1).equals("if")
> -                || strDirective.substring(1).equals("end")
> -                || strDirective.substring(1).equals("set")
> -                || strDirective.substring(1).equals("else")
> -                || strDirective.substring(1).equals("elseif")
> -                || strDirective.substring(1).equals("stop")
> +            if ( directive.equals("if")
> +                || directive.equals("end")
> +                || directive.equals("set")
> +                || directive.equals("else")
> +                || directive.equals("elseif")
> +                || directive.equals("stop")
>            )
>            {
>                bRecognizedDirective = true;
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 


Good one.  Thx

-- 
Geir Magnusson Jr. 
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>