-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[core] use StringComparison.Ordinal everywhere #4988
[core] use StringComparison.Ordinal everywhere #4988
Conversation
90c46f7
to
e11fe7d
Compare
The rules in the |
bf94fa4
to
f2f04bc
Compare
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.
A couple of places it looks like it's attempting to StringComparison for char comparisons.
Context: https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1307 Context: https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1309 Context: dotnet/runtime#43956 I was reviewing `dotnet trace` output of the .NET Podcast app: 6.32ms Microsoft.Maui.Controls!Microsoft.Maui.Controls.ShellNavigationManager.GetNavigationState 3.82ms Microsoft.Maui.Controls!Microsoft.Maui.Controls.ShellUriHandler.FormatUri The bulk of this time is spent in `string.StartsWith()`, doing a culture-aware string comparison? 3.82ms System.Private.CoreLib!System.String.StartsWith 2.57ms System.Private.CoreLib!System.Globalization.CultureInfo.get_CurrentCulture This looks to be showing the cost of the 1st culture-aware comparision plus any time making these slower string comparisons. It would be ideal if project templates did not even hit `CultureInfo.get_CurrentCulture`. To solve this, let's add to the `.editorconfig` file: dotnet_diagnostic.CA1307.severity = error dotnet_diagnostic.CA1309.severity = error And then fix all the places errors appear. There are some complications in projects that use `netstandard2.0`: 1. For `Contains()` use `IndexOf()` instead. 2. Use `#if NETSTANDARD2_0` for `GetHashCode()` or `Replace()`. 3. Generally, `InvariantCulture` should not be used. After these changes, the `FormatUri` method disappears completely from the trace, and we instead get: 2.88ms Microsoft.Maui.Controls!Microsoft.Maui.Controls.ShellNavigationManager.GetNavigationState I suspect this saves ~3.44ms for any MAUI app startup, and a small amount more depending on the number of string comparisions happening.
f2f04bc
to
22944bf
Compare
@@ -91,7 +91,7 @@ protected override async Task HandleWebResourceRequest(CoreWebView2WebResourceRe | |||
{ | |||
relativePath = _hostPageRelativePath; | |||
} | |||
relativePath = Path.Combine(_contentRootDir, relativePath.Replace("/", "\\")); | |||
relativePath = Path.Combine(_contentRootDir, relativePath.Replace('/', '\\')); |
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.
Context: https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1307
Context: https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1309
Context: dotnet/runtime#43956
I was reviewing
dotnet trace
output of the .NET Podcast app:The bulk of this time is spent in
string.StartsWith()
, doing aculture-aware string comparison?
This looks to be showing the cost of the 1st culture-aware comparision
plus any time making these slower string comparisons. It would be
ideal if project templates did not even hit
CultureInfo.get_CurrentCulture
.To solve this, let's add to the
.editorconfig
file:And then fix all the places errors appear.
There are some complications in projects that use
netstandard2.0
:For
Contains()
useIndexOf()
instead.Use
#if NETSTANDARD2_0
forGetHashCode()
orReplace()
.Generally,
InvariantCulture
should not be used.After these changes, the
FormatUri
method disappears completely fromthe trace, and we instead get:
I suspect this saves ~3.44ms for any MAUI app startup, and a small
amount more depending on the number of string comparisions happening.