-
I'm working on a source generator implementation that uses Alexandre Mutel's Scriban template engine to provide a T4-like templating system. This source generator consumes scriban templates via AdditionalFiles, which it then processes into SourceText. It does not, in any way, interrogate the source syntax trees. I'm trying to determine if SourceGenerators are a good fit for this use-case or not. The question I have is about performance and caching. Since the source generator is run frequently in VS at design time, any slowness will become immediately apparent to the user. The template can potentially generate an tremendous amount of source code, which Roslyn then needs to parse into a syntax tree. I was under the impression that there is caching and re-use of the syntax tree if the sourcetext hasn't changed. However, the source generator will still be called frequently, so I was thinking it would make sense to cache the output of the template transformation as well, since it only needs to be re-processed if the source template file has change. I've added this caching to the source generator using the template file timestamp as the cache token, but it isn't clear if this is preventing Roslyn from doing unnecessary work. My cache is holding on to the SourceText instance itself, so I'm returning the same instance from each invocation of However, even with the caching enabled the My work is at: https://github.com/MarkPflug/scriban/tree/sourcegen I hope this description makes sense. I'd be happy to clarify any points. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
I've transferred this to roslyn, as it's not a csharplang question. @chsienki, can you give advice? |
Beta Was this translation helpful? Give feedback.
-
This is exactly the approach used by source generators in both dotnet/roslyn and DotNetAnalyzers/StyleCopAnalyzers. It's not the only good use case, but it's one where we would expect a good experience.
These are the two primary optimizations under current investigation by @jasonmalinowski. I believe the current expectations (which are not yet implemented but hopefully are soon) are:
tl;dr: You are currently using the best known approach, but the optimizations to fully realize the gains are not yet implemented. As the implementation is completed, you will automatically observe the resulting performance improvements in IDE scenarios with your source generator. |
Beta Was this translation helpful? Give feedback.
-
@333fred I figured out how I accidentally ended up in the CSharpLang repo. I was going down the path of opening an issue (didn't realize discussions existed), and clicked the "Suggest language feature" "Open" button (because the other options didn't seem appropriate) and didn't notice that it moved me to the CSharpLang repo. At that point "Open a Discussion" looked appropriate, so that's how this ended up over there. Y'all might want to add a "Open a Discussion" link here in the Roslyn repo as well, as I would have clicked that if it was in the Issues list here. |
Beta Was this translation helpful? Give feedback.
-
I have a question in this topic. If I’m generating code that is basing on Roslyn tree of some class – for example generating partial class implementation. Is there any way to get some kind of hashcode that identifies this class so when class is not changed this hascode is not changed and I know I could use previously generated code? |
Beta Was this translation helpful? Give feedback.
This is exactly the approach used by source generators in both dotnet/roslyn and DotNetAnalyzers/StyleCopAnalyzers. It's not the only good use case, but it's one where we would expect a good experience.
These are the two primary optimizations under curren…