<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>I&#8217;m extremely interested to see how the compiled code performs
compared to my C# port of StringTemplate.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>That aside, I think the idea of compiling templates to an
assembly is great. This is the kind of program that truly makes use of the multi-language
ability at the core of the CLI. I don&#8217;t have profiling results from the
Java version of StringTemplate to compare with, but I&#8217;ll give you an overview
of the results of some profiling I did.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>I used a sampling method to profile the library. The test was a
batch conversion of the 8 grammars that are used in the C# port of the ANTLR
Tool. 54.33% (inclusive) of the samples were in Antlr3.StringTemplate.dll. Only
6.59% (inclusive) was spent in ASTExpr.rawGetObjectProperty, which is
responsible for all measurable uses of .NET reflection. 35.31% (inclusive) of
the time was spent in ActionEvaluator.action, (note: this call encapsulates all
calls to ASTExpr.rawGetObjectProperty).<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>By implementing a System.Type -&gt; System.Reflection.MemberInfo
map in ASTExpr and caching the property accessors, I was able to reduce the
time spent in ASTExpr.rawGetObjectProperty to 2.36% (inclusive).<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Sam<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>PS: I&#8217;ve thought about doing something similar to the CLI
compilation with ANTLR grammars, but profiling the generated code has shown
that it&#8217;s not the bottleneck right now.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'>

<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>
stringtemplate-interest-bounces@antlr.org
[mailto:stringtemplate-interest-bounces@antlr.org] <b>On Behalf Of </b>Volkan
Ceylan<br>
<b>Sent:</b> Saturday, February 28, 2009 4:44 PM<br>
<b>To:</b> stringtemplate-interest@antlr.org<br>
<b>Subject:</b> [stringtemplate-interest] StringTemplate Compiler for .NET<o:p></o:p></span></p>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='margin-bottom:12.0pt'>Hello ST lovers,<br>
<br>
I met StringTemplate about two years ago and before that i had never used a templating
language (considering my 15+ years coding history, its a shame :), other than
String.Format. Now, i have a CMS developed in C#/.NET that completely depends
on ST to generate pages, where templates are stored in a SQL database. It is
actively being used in some important web sites in my country. I love the idea
behind ST to enforce model-view separation, which cleared the messy HTML markup
from my C# classes completely, and made it easier to change the look of
interface based on customer requests. I strongly recommend ST to any .NET
developer. Even MS has seen the light and invented MVC framework but they are a
bit late. Anyway, ST can be used as an alternative view engine in MVC framework
too, as the default one doesn't still seem functional enough to me :)<br>
<br>
Nowadays, i am working on a high traffic web site where we expect to take
thousands of requests per second. A page with a rather complex template (or set
of templates) takes about 35 ms (after some optimizations, it was 130 ms at
start) just for StringTemplate.ToString(). This time doesn't include the time
required to parse a template (templates are parsed and cached, and i'm using
ST.GetInstanceOf()), and it also doesn't include the time for populating
template with data. Fetching all data required in the page from SQL and setting
them in ST takes only 6 ms. <br>
<br>
At first sight, 35 ms may seem a small amount of time, but it means that only
about 25 requests per second can be handled by the web server if we had just
called ST.ToString() to render a page, so the actual requests per second is
even lower.<br>
<br>
As i had no intention to give up on StringTemplate, decided to try writing a
compiler for it. I've set up a project in Google code. <br>
<br>
<a href="http://code.google.com/p/stringtemplate-dotnet-compiler/">http://code.google.com/p/stringtemplate-dotnet-compiler/</a><br>
<br>
There is a working compiler in SVN repository, but don't expect much from it.
It is only two days of work yet, not even in alpha state.<br>
<br>
For now, it can only compile templates like &quot;testing $a:{$it:{$it:{$it:{$it$}$}$}$}$
$a+b+&quot;blablalalbla&quot;$ $b$ $c$ $d$ end&quot;<br>
<br>
No formal arguments, no groups, no format/anchor/null options, no no no, just
what you see above :)<br>
<br>
When it comes to speed, for this simple template it works about 5 times faster
than native StringTemplate. I'm expecting the difference to grow as templates
gets more complex. <br>
Also, there are some room for optimizations, such as inlining simple template
chunks and constant expressions, but my priority is to create a fully
compilant, working compiler.<br>
<br>
To keep compiler simple, and easier to develop, i decided not to parse
templates myself, but take a StringTemplate object, walk its chunks and trees
and emit IL code instead of evaluating expressions. For example, i took
ActionEvaluator.cs from ST source code, and replaced evaluation expressions
with dynamic IL generating code. Result can be seen in ActionCompiler.cs.<br>
<br>
I'd appreciate any suggestions, ideas, help etc. Would like to hear from
Terrence (thanks a lot for letting me see the light :), opinion as i read in
the group he also has plans to turn StringTemplate into a code generating
compiler in v4.<br>
<br>
Volkan Ceylan<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<o:p></o:p></p>

</div>

</body>

</html>