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
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.