-
Notifications
You must be signed in to change notification settings - Fork 480
[Performance] Improve SpecifyIFormatProvider (CA1305) performance #6865
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,11 +186,11 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context | |
|
|
||
| #region "IFormatProviderAlternateStringRule Only" | ||
| if (stringFormatMemberWithIFormatProviderStringAndParamsObjectParameter != null && | ||
| !oaContext.Options.IsConfiguredToSkipAnalysis(IFormatProviderAlternateStringRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation) && | ||
| (targetMethod.Equals(stringFormatMemberWithStringAndObjectParameter) || | ||
| targetMethod.Equals(stringFormatMemberWithStringObjectAndObjectParameter) || | ||
| targetMethod.Equals(stringFormatMemberWithStringObjectObjectAndObjectParameter) || | ||
| targetMethod.Equals(stringFormatMemberWithStringAndParamsObjectParameter))) | ||
| targetMethod.Equals(stringFormatMemberWithStringAndParamsObjectParameter)) && | ||
| !oaContext.Options.IsConfiguredToSkipAnalysis(IFormatProviderAlternateStringRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation)) | ||
| { | ||
| // Sample message for IFormatProviderAlternateStringRule: Because the behavior of string.Format(string, object) could vary based on the current user's locale settings, | ||
| // replace this call in IFormatProviderStringTest.M() with a call to string.Format(IFormatProvider, string, params object[]). | ||
|
|
@@ -266,19 +266,16 @@ protected override void InitializeWorker(CompilationStartAnalysisContext context | |
|
|
||
| if (!oaContext.Options.IsConfiguredToSkipAnalysis(uiCultureRule, targetMethod, oaContext.ContainingSymbol, oaContext.Compilation)) | ||
| { | ||
| IEnumerable<int> IformatProviderParameterIndices = GetIndexesOfParameterType(targetMethod, iformatProviderType); | ||
| foreach (var index in IformatProviderParameterIndices) | ||
| foreach (var argument in invocationExpression.Arguments) | ||
| { | ||
| var argument = invocationExpression.Arguments[index]; | ||
|
|
||
| if (argument != null && currentUICultureProperty != null && | ||
| installedUICultureProperty != null && currentThreadCurrentUICultureProperty != null) | ||
| if (!iformatProviderType.Equals(argument.Parameter?.Type)) | ||
| { | ||
| var semanticModel = argument.SemanticModel!; | ||
|
|
||
| var symbol = semanticModel.GetSymbolInfo(argument.Value.Syntax, oaContext.CancellationToken).Symbol; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mavasani I assume semantic model calls are more expensive than getting info directly from IOperation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, definitely. |
||
| continue; | ||
| } | ||
|
|
||
| if (symbol != null && | ||
| if (currentUICultureProperty != null && installedUICultureProperty != null && currentThreadCurrentUICultureProperty != null) | ||
| { | ||
| if (argument.Value.WalkDownConversion() is IPropertyReferenceOperation { Property: { } symbol } && | ||
| (symbol.Equals(currentUICultureProperty) || | ||
| symbol.Equals(installedUICultureProperty) || | ||
| symbol.Equals(currentThreadCurrentUICultureProperty) || | ||
|
|
@@ -324,14 +321,6 @@ static IEnumerable<IMethodSymbol> GetToStringMethods(INamedTypeSymbol namedTypeS | |
| => namedTypeSymbol.GetMembers("ToString").OfType<IMethodSymbol>().WhereNotNull(); | ||
| } | ||
|
|
||
| private static IEnumerable<int> GetIndexesOfParameterType(IMethodSymbol targetMethod, INamedTypeSymbol formatProviderType) | ||
| { | ||
| return targetMethod.Parameters | ||
| .Select((Parameter, Index) => (Parameter, Index)) | ||
| .Where(x => x.Parameter.Type.Equals(formatProviderType)) | ||
| .Select(x => x.Index); | ||
| } | ||
|
Comment on lines
-327
to
-333
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm now looping over arguments directly and than checking if the parameter is of interest. The new implementation should hopefully be more performant and more straightforward. |
||
|
|
||
| private static ParameterInfo GetParameterInfo(INamedTypeSymbol type, bool isArray = false, int arrayRank = 0, bool isParams = false) | ||
| { | ||
| return ParameterInfo.GetParameterInfo(type, isArray, arrayRank, isParams); | ||
|
|
||
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.
Symbol equality should be faster than
IsConfiguredToSkipAnalysis