Reduce allocations in EnumsShouldHaveZeroValueAnalyzer #50411
+103
−37
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I would like to suggest PR that is reducing allocations
EnumsShouldHaveZeroValueAnalyzer
:Currently the analyzer allocates
ImmutableArray
to 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.GetZeroValueFields
method allocates two enumerators withWhere
andCast
calls, they can be removed.When the analyzer gets zero value field names from analyzer options it allocates
string[]
withstring.Split
,ImmutableArray
and also a delegate with closure. All those allocations can be removed, particularlystring.Split
call can be replaced withSpan
-based splitting.There are new
MemoryExtensions.Split<T>
methods introduced in .NET 9, but they are unavailable for analyzers since they targetnetstandard2.0
. I would probably suggest to provide aninternal
helper as a simplified version ofMemoryExtensions.Split<T>
that could be used in analyzers. In this PR, I used it inEnumsShouldHaveZeroValueAnalyzer
to reduce allocations.