csf-dev/ZPT-Sharp

To improve performance, compile-time code generation should be used for C# expressions

craigfowler opened this issue · 3 comments

Presently, C# expressions within ZPT templates are compiled at runtime, upon first usage. This incurs a small performance penalty at runtime for each expression.

I would like to look into the use of Roslyn code generation to move this process to compile-time, for template files which are available when the app is compiled.

It may still be required to support runtime compilation for any templates which are not available at compile-time. I also wonder how this would interact with technologies such as hot reload.

This would require the whole ZptSharp process to be invoked during compilation. It's have to scaffold the engine in order to parse and compile (pre-compile) the templates which are used as views.

This would be an optional feature in the build process. Probably I would control it via some property or item group within the csproj file. If the pre-compilation feature is enabled (via a property?) then item values would indicate file path(s)/patterns in which to search for ZPT template files in order to pre-compile.

For MVC apps likely we'd switch this on by default and pre-compile templates found in the views directory. We could detect this by detecting package references for ASP.NET (Core) MVC ?

I can see this actually being difficult, perhaps unworkable.

The runtime behaviour of templates, and the combination of variable scopes/how templates consume one another might be unknowable at compile-time.

This would make it - perhaps excessively - difficult to determine which expressions require compilation at compile-time. It might be best to leave the compilation of expressions to runtime (with caching).

Closing as "wontfix". Per my last comment, there is no way to sensibly predict all of the possible runtime combinations of the template files and how the variable scope could change.