Shared code for source generators #79741
-
Source generators are super powerful, but at the moment require a large amount of work at the syntax level to be able to read and then generate the required code. I think a lot of cases will end up duplicating this code but with the desire of pretty much doing the same things over and over again, especially things like serializers. Are there any plans to export a fast source generated variant of reflection that can be used by multiple reflection systems? Even better would be a simple attribute that then accelerates the current reflection APIs by producing concrete implementation for that type but I can see that those may not be optimized for performance... Things like System.Text.Json could then reuse the common generated API but so could others, in the end reducing the amount of code that needs to be generated For instance we have a custom value system that we want to be convertable to .NET objects but writing a source generator would be a huge amount of work and doing it via reflection is likely to be slow! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
For existing reflection APIs, there is opportunity to improve performance. .NET 7 had improvements in this space (https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#reflection) and some more are planned for .NET 7. The performance ceiling of the existing reflection APIs is given by the API shape - the reflection APIs take and return boxed values, perform a lot of validations and conversions, etc. Reflection is always going to be significantly slower than straight-line core. Source generators cannot really fix that.
What would the common generated API be? There is a lot of System.Text.Json specific details in the code that the System.Text.Json generates today. |
Beta Was this translation helpful? Give feedback.
For existing reflection APIs, there is opportunity to improve performance. .NET 7 had improvements in this space (https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#reflection) and some more are planned for .NET 7.
The performance ceiling of the existing reflection APIs is given by the API shape - the reflection APIs take and return boxed values, perform a lot of validations and conversions, etc. Reflection is always going to be significantly slower than straight-line core. Source generators cannot really fix that.
What would the common generated API be? There is a lot of System.Text.Json specific details i…