[stringtemplate-interest] StringTemplate Compiler for .NET

Sam Harwell sharwell at pixelminegames.com
Thu Mar 19 02:20:32 PDT 2009


You have a lot of useful info there. Out of curiosity, I'm wondering how
fast this type of code would be. It works with the debugger and
IntelliSense tools better.

 

public

ifConditionFunctional returns
[System.Func<ASTExpr,StringTemplate,IStringTemplateWriter,bool> func]

        :       ifAtomFunctional

                {$func = $ifAtomFunctional.func;}

        |       ^(NOT ifAtomFunctional)

                {$func = (chunk,self,writer) =>
!($ifAtomFunctional.func(chunk,self,writer));}

        ;

 

ifAtomFunctional returns
[System.Func<ASTExpr,StringTemplate,IStringTemplateWriter,bool> func]

        :       exprFunctional

                {$func = (chunk,self,writer) =>
chunk.TestAttributeTrue($exprFunctional.func(chunk,self,writer));}

        ;

 

exprFunctional returns
[System.Func<ASTExpr,StringTemplate,IStringTemplateWriter,object> func]

        :       ^(PLUS a=exprFunctional b=exprFunctional)

                {$func = (chunk,self,writer) =>
chunk.Add($a.func(chunk,self,writer),$b.func(chunk,self,writer));}

        |       templateApplicationFunctional

                {$func = $templateApplicationFunctional.func;}

        |       attributeFunctional

                {$func = $attributeFunctional.func;}

        |       templateIncludeFunctional

                {$func = $templateIncludeFunctional.func;}

        |       functionFunctional

                {$func = $functionFunctional.func;}

        |       listFunctional

                {$func = $listFunctional.func;}

        |       ^(VALUE a=exprFunctional)

                {

                        $func = (chunk,self,writer) =>

                                {

                                        var value =
$a.func(chunk,self,writer);

                                        StringWriter buf = new
StringWriter();

                                        IStringTemplateWriter sw =
self.Group.GetStringTemplateWriter( buf );

                                        int n = chunk.WriteAttribute(
self, value, sw );

                                        if ( n > 0 )

                                                return buf.ToString();

                                        return value;

                                };

                }

        ;

 

Sam

 

-----Original Message-----
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of Volkan
Ceylan
Sent: Thursday, March 19, 2009 3:24 AM
To: stringtemplate-interest at antlr.org
Subject: Re: [stringtemplate-interest] StringTemplate Compiler for .NET

 

> So here we go/you've inspired me. :) For syntax highlighting, view in
HTML.

 

Wouv i'm glad, thats great,  ;)

 

I still don't have access to repository and can't see actual code.

Prof. Parr could you please set up a readonly account for me to access

ST#3 code?

 

Btw, it would be great if expressions are compiled during parsing. As

i didn't want to modify ST code myself, i choosed another way. I'm

creating a set of dynamic methods/CompiledTemplate objects given a set

of StringTemplate/StringTemplateGroup objects. So when the template is

going to be converted to string with a new set of attributes by means

of compiledTemplate.GetInstanceOf().ToString(), there won't be any

dynamic compilation at this stage, just calls to dynamic methods. You

are not generating dynamic methods on evaluation stage each time a

template instance is converted to string, do you? It would be very

slow, because of the way ILGenerator works (probably you don't, i

couldn't have enough time to review code yet).

 

I would like to point in some performance bottlenecks that i

discovered during my development. Might be helpful if you are planning

to keep on doing compilations / optimizations.

 

- Anonymous templates (and most named templates) usually only have one

(or none) argument, "it" or a named variant of it e.g. "x". And there

is also

"i" and "i0" special variables. Even when a template access only these

attributes, a dictionary lookup is still required. And dictionary

lookup is slow (yes O(1) but still 10x slower than a direct variable

lookup). They should be treated specially in special template

variables. I did this for compiled templates and it helped a lot.

Templates with a single argument can also be treated specially.

 

- Options like separator, format, null, anchor etc. are all string

templates, even when they just contains of a constant simple string

(And most of the time like %99 they are constants). When they are

constants, they should also be treated specially.

 

- There is a list called EmbeddedInstances in each StringTemplate.

This list is not used anywhere, grows each time a template is

converted to a string and it prevents garbage collection of template

instances. Don't know if your port has them, but if it does, should be

removed.

 

- ASTExpr.ApplyListOfAlternatingTemplates is called even when there is

only one template and it has array lookup/modulus overheads. Also it

creates an array of templates for each element and returns that array

as results may be applied to another template, but most of the time

they don't. May be optimized by looking ahead in the expression tree

to see if there is an upcoming template application.

 

Volkan Ceylan

_______________________________________________

stringtemplate-interest mailing list

stringtemplate-interest at antlr.org

http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20090319/cadc9e97/attachment-0001.html 


More information about the stringtemplate-interest mailing list