[stringtemplate-interest] StringTemplate Compiler for .NET
Sam Harwell
sharwell at pixelminegames.com
Sun Mar 22 06:21:45 PDT 2009
When you hit a breakpoint in Visual Studio, ToString is called
automatically during Watch/Locals evaluation. I need to handle this
cleanly.
Freezing can be done implicitly or explicitly. A frozen template can be
modified by cloning it and working with the clone. Implicit freezing
occurs when ToString() is called on a non-frozen template: a frozen
clone is created, then that clone is evaluated to return the string.
Explicit cloning occurs via a call to Freeze (freezes the current
object) or GetAsFrozen (returns a frozen clone of the object, copying by
reference where possible).
I'm using Func<T> or the IL Generator for my codegen methods. The
Func<T> method is currently working since it easily expresses functional
languages and can be debugged, but it may be slower than IL gen in the
end due to more difficult inlining which I haven't implemented yet.
Profiling showed that current bottlenecks are not in either building the
functional representation or in evaluating the functor itself.
Sam
-----Original Message-----
From: Johannes Luber [mailto:jaluber at gmx.de]
Sent: Saturday, March 21, 2009 6:31 PM
To: Sam Harwell
Cc: Volkan Ceylan; stringtemplate-interest at antlr.org
Subject: Re: [stringtemplate-interest] StringTemplate Compiler for .NET
Sam Harwell schrieb:
> This email describes a proposal that I'm absolutely willing to
> implement. As I mention at the end, I'm very interested in feedback on
> the idea as will.
>
> Before I work to optimize handling of various cases, I'm modifying
> StringTemplate to have a Frozen (immutable) state. Before the object
is
> frozen, a call to ToString() will internally call GetAsFrozen()
> (actually not quite; see #4 below) to get a frozen duplicate of the
> current template, and evaluate ToString() on it. A warning will
> optionally be issued to stderr when this case occurs.
I don't get how a non-frozen template can return the correct results, if
you say that this should be considered an error. Furthermore, at which
point are templates frozen? Is this done automatically? If yes, how can
you be sure of the correct time? What happens if someone wants to modify
a frozen template? Does this result in an error or do return a modified
clone (with set attributes)?
>
> When a template is frozen, all attributes are locked and the
expression
> tree is compiled. However, a subsequent call to Clone will return an
> non-frozen copy of the template with no attributes set. The advantage
> comes from several key characteristics of the cloned copies:
Apropos compilation: You said you are using the codedom, right? Isn't
that too buggy for more than academic use?
>
> 1. The clone gets a copy of the compiled expression tree. As long as
the
> pattern is not changed, the expression tree will not need recompiling
> before evaluation. The calls to Freeze or GetAsFrozen will simply lock
> the attribute set.
>
> 2. When the expression tree is compiled, a List of objects is created
> with a bucket for every referenced attribute of every template. When
> SetAttribute is called, the value is placed immediately in the proper
> bin for direct lookup during evaluation. Otherwise, it goes in the
> dictionary and the values are placed in bins after the expression is
> compiled.
I don't get item two without any examples... Otherwise your design seems
to be quite sound.
Johannes
More information about the stringtemplate-interest
mailing list