[stringtemplate-interest] StringTemplate Compiler for .NET
Volkan Ceylan
volkanceylan at gmail.com
Sat Mar 21 03:39:00 PDT 2009
Hi again,
Thanks a lot Prof. Parr, now i can see Sam's code.
It will take some time before i understand your work fully, but seems
like you are following a similar path with me,
emitting dynamic methods in ActionEvaluator.cs but certainly
proceeding a lot faster than me. If you finish your
work, mine will probably become obsolete but anyway i'll keep on
working, its a practice and brainstorming for me
to perform in evenings :)
Yesterday i made a profiling session to find out why it is working so
slow in StringTemplate.ToString() or
StringTemplate.Write methods using my templates. It seemed like most
calls are to new HashTable(),
new StringTemplate(), StringTemplate.BreakTemplateIntoChunks(),
GetConstructorInfo() methods. I can understand
new StringTemplate() calls caused by GetInstanceOf() methods but why
BreakTemplateIntoChunks()??
It shouldn't be parsing templates in evaluation stage.
In ActionEvaluator.attribute(), for ANONYMOUS_TEMPLATE nodes, there is
a code like below:
StringTemplate valueST = new StringTemplate(self.Group, at.GetText());
valueST.EnclosingInstance = self;
valueST.Name = "<anonymous template argument>";
value = valueST;
This is called for all anonymous templates and all separator, format,
null etc. options in each evaluation.
Caching the template by replacing code with:
if (at.StringTemplate == null)
{
at.StringTemplate = new StringTemplate(at.getText());
at.StringTemplate.Name = "<anonymous template argument>";
}
StringTemplate valueST = at.StringTemplate.GetInstanceOf();
valueST.EnclosingInstance = self;
valueST.Group = self.Group;
value = valueST;
removed all these parsing calls, and it saved a lot time alone as
parsing is slow.
Your port seems to have the same code block causing problem.
Reflection method GetConstructorInfo() was called in BreakTemplateIntoChunks()
while creating an instance of lexer class but seems like you spotted
that performance bottleneck.
There is also another potential problem i spotted while working on the compiler:
AFAIK, StringTemplate.ToString() calls (i mean evaluation) is
thread-safe, as long as you
use a unique instance created with GetInstanceOf() methods. I see no
reason why it should't.
But in just a few places in ActionEvaluator.cs for ANONYMOUS_TEMPLATE nodes,
and in ApplyTemplateToListOfAttributes, ApplyAlternatingTemplates
methods, an instance of
anonymous template is not used (by calling of GetInstanceOf()), but
directly anonymous
template itself is modified (this is not true for named templates,
always a new instance of
them is used). This will cause same anonymous template instance to be modified
in different threads. If threads modify at same time, this may cause
unexpected side-effects.
Am i wrong in assuming string template instance evaluation as
thread-safe or this is a bug??
2009/3/20 Sam Harwell <sharwell at pixelminegames.com>:
> I checked in (CL5952) an update that builds a delegate (System.Func<T>)
> inside ActionEvaluator instead of basic interpretation. I have not
> implemented caching of the delegates or optimized resolution of
> arguments other special name attributes. However, the implementation is
> a complete working example of execution of a compiled template that
> passes the same 1200 unit tests that the interpreter does.
More information about the stringtemplate-interest
mailing list