Reduce allocations in EnumsShouldHaveZeroValueAnalyzer#50411
Reduce allocations in EnumsShouldHaveZeroValueAnalyzer#50411Alex-Sob wants to merge 10 commits intodotnet:mainfrom
Conversation
|
|
||
| internal static class SpanExtensions | ||
| { | ||
| public static SpanSplitEnumerator Split(this ReadOnlySpan<char> source, char separator) |
There was a problem hiding this comment.
@stephentoub There are new MemoryExtensions.Split<T> methods introduced in .NET 9, but they are unavailable for analyzers since they target netstandard2.0. Do you think it's ok to provide a simplified version to be used for analyzers? For example, it could be used in EnumsShouldHaveZeroValueAnalyzer and considerably reduce allocations. By the way, it seems that Range and Index here are local versions of respective BCL types.
There was a problem hiding this comment.
It seems like there's no test project for Analyzer.Utilities projects, so I'm not sure where to add unit tests.
|
Due to lack of recent activity, this PR has been labeled as 'Stale'. It will be closed if no further activity occurs within 7 more days. Any new comment will remove the label. |
I would like to suggest PR that is reducing allocations
EnumsShouldHaveZeroValueAnalyzer:Currently the analyzer allocates
ImmutableArrayto store zero value fields of an enum type. It can be removed, in the case when there are multiple zero value fields the analyzer just needs to report an error, it can use a flag for that.GetZeroValueFieldsmethod allocates two enumerators withWhereandCastcalls, they can be removed.When the analyzer gets zero value field names from analyzer options it allocates array for separator
new[] { '|' },string[]and multiplestringparts withstring.Split,ImmutableArrayand also a delegate with closure. All those allocations can be removed, particularlystring.Splitcall can be replaced withSpan-based splitting.In .NET 8+ there are
MemoryExtensions.Splitmethods to split strings that areSpan-based, but they are unavailable for analyzers since they targetnetstandard2.0. What about providing someSpan-based helpers that could be used in analyzers? I'm proposing to add such a helper in [Proposal] Use Span-based methods to split strings in analyzers #50581. In this PR I'm using the same helper inEnumsShouldHaveZeroValueAnalyzer.