[stringtemplate-interest] StringTemplate Compiler for .NET
Volkan Ceylan
volkanceylan at gmail.com
Sat Feb 28 14:44:12 PST 2009
Hello ST lovers,
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 :)
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.
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.
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.
http://code.google.com/p/stringtemplate-dotnet-compiler/
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.
For now, it can only compile templates like "testing
$a:{$it:{$it:{$it:{$it$}$}$}$}$ $a+b+"blablalalbla"$ $b$ $c$ $d$ end"
No formal arguments, no groups, no format/anchor/null options, no no no,
just what you see above :)
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.
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.
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.
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.
Volkan Ceylan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20090301/a6eae078/attachment.html
More information about the stringtemplate-interest
mailing list