-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Reduce allocations in EnumsShouldHaveZeroValueAnalyzer #50411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
||
internal static class SpanExtensions | ||
{ | ||
public static SpanSplitEnumerator Split(this ReadOnlySpan<char> source, char separator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like there's no test project for Analyzer.Utilities
projects, so I'm not sure where to add unit tests.
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 array for separator
new[] { '|' }
,string[]
and multiplestring
parts withstring.Split
,ImmutableArray
and also a delegate with closure. All those allocations can be removed, particularlystring.Split
call can be replaced withSpan
-based splitting.In .NET 8+ there are
MemoryExtensions.Split
methods 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
.