次の方法で共有


Caching in latest T4 drop

Adam Miller has been using our T4 engine and has a question about caching...

A caching question: Using the standard templating engine (Microsoft.VisualStudio.TextTemplating.Engine),  is there a way to cache the same template file between generations? Specifically, the engine.ProcessTemplate seems to recompile the template each time. Is there a way to cache it? It's taking us minutes to generate 200-300 templates all based on a single template file with  different arguments being passed to it.

The code is as easy as: engine.ProcessTemplate(contentStr, new TemplateHost(".", templateArgs).

In our latest CTP, we've implemented a basic caching scheme which not only speeds up generation from DSL models with Visual Studio, but allows you to speed up your own Visual Studio extensibility projects that take advantage of the T4 engine.

We've implemented a caching scheme based on the full content of the template, including all files included with an <#@ include #> directive and, critically, all of the code injected by any directive processors.  If that set of pieces catenated as a string hashes the same then we'll attempt to reuse the compiled transformation class from a previous run.  We're using a 128-bit MD5 hash right now.  If anyone has any collision problems with that bit depth, I'd like to hear from them.

The cache is cooperative between the T4 engine and its host.  To turn on the caching features, you'll need to implement the following new method in the hosting API as follows:

public object GetHostOption(string optionName)

If optionName is "CacheAssemblies" and you return boolean true, then the engine will reuse generated transformation code rather than compiling it every time. If you simply return null on this call, the previous non-caching behavior will be preserved.

Obviously, as with any hashing-only solution, your host should provide an end-user toggleable switch to turn off the cache just in case someone is unlucky enough to hit a collision.
In the DSL Tools' VS-hosted T4 implementation, the switch to turn off caching is in the registry. If you add a string value CacheAssemblies="false" to the key

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\TextTemplating\

then you'll turn off caching.

Adam, I hope this new feature meets your requirements. I'd be especially interested to know if your argument-passing scheme works with this implementation or defeats it. Please do let me know how you get on with it.

Don't forget, our latest CTP is also the first public bow for the changes George mentioned some time ago around features to make building up libraries of code generation templates much easier.

Comments

  • Anonymous
    June 05, 2006
    I've been using the TemplateHost class located in the Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates library. In addition, I'm using my own templates, etc. instead of any of the DSL stuff. When looking at the TemplateHost class, it doesn't contain the new GetHostOption method noted above. I'm worried that this means I need to write my own TemplateHost class, (and started with the example under the ITextTemplatingEngineHost Interface section of the SDK, but I feel like I'm reinventing the wheel.) I could probably subclass also and just add the necessary method above but again - I feel like I'm missing something. Any suggestions on where to go from here?
  • Anonymous
    June 05, 2006
    The comment has been removed
  • Anonymous
    June 06, 2006
    The comment has been removed
  • Anonymous
    June 08, 2006
    The comment has been removed
  • Anonymous
    July 20, 2006
    The comment has been removed
  • Anonymous
    July 21, 2006
    The comment has been removed
  • Anonymous
    June 04, 2009
    Mike Pagel recently posted an interesting article about T4 on CodeProject called &#8220; T4: Extending