-
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 StringComparer for Dictionary/HashSet #14900
[core] use StringComparer for Dictionary/HashSet #14900
Conversation
Context: dotnet#12130 Context: https://github.com/angelru/CvSlowJittering When profiling the above customer app while scrolling, 4% of the time is spent doing dictionary lookups: (4.0%) System.Private.CoreLib!System.Collections.Generic.Dictionary<TKey_REF,TValue_REF>.FindValue(TKey_REF) Observing the call stack, some of these are coming from culture-aware string lookups in MAUI: * `microsoft.maui!Microsoft.Maui.PropertyMapper.GetProperty(string)` * `microsoft.maui!Microsoft.Maui.WeakEventManager.AddEventHandler(System.EventHandler`1<TEventArgs_REF>,string)` * `microsoft.maui!Microsoft.Maui.CommandMapper.GetCommand(string)` Which show up as a mixture of `string` comparers: (0.98%) System.Private.CoreLib!System.Collections.Generic.NonRandomizedStringEqualityComparer.OrdinalComparer.GetHashCode(string) (0.71%) System.Private.CoreLib!System.String.GetNonRandomizedHashCode() (0.31%) System.Private.CoreLib!System.Collections.Generic.NonRandomizedStringEqualityComparer.OrdinalComparer.Equals(string,stri (0.01%) System.Private.CoreLib!System.Collections.Generic.NonRandomizedStringEqualityComparer.GetStringComparer(object) In cases of `Dictionary<string, TValue>` or `HashSet<string>`, we can use `StringComparer.Ordinal` for faster dictionary lookups. Unfortunately, there is no code analyzer for this: dotnet/runtime#52399 So, I manually went through the codebase and found all the places. I now only see the *fast* string comparers in this sample: (1.3%) System.Private.CoreLib!System.Collections.Generic.NonRandomizedStringEqualityComparer.OrdinalComparer.GetHashCode(string) (0.35%) System.Private.CoreLib!System.Collections.Generic.NonRandomizedStringEqualityComparer.OrdinalComparer.Equals(string,stri Which is about ~0.36% better than before. This should slightly improve the performance of handlers & all controls on all platforms. I also fixed `Microsoft.Maui.Graphics.Text.TextColors` to use `StringComparer.OrdinalIgnoreCase` -- and removed a `ToUpperInvariant()` call.
Maybe add a TODO comment in the editorconfig file to enforce an error whenever such a code analyzer shows up, and link that open issue. |
@symbiogenesis since dotnet/runtime#52399 was opened in 2019, I don't know when (if ever) it will be completed. Or is there a |
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: #12130
Context: https://github.com/angelru/CvSlowJittering
When profiling the above customer app while scrolling, 4% of the time is spent doing dictionary lookups:
Observing the call stack, some of these are coming from culture-aware string lookups in MAUI:
microsoft.maui!Microsoft.Maui.PropertyMapper.GetProperty(string)
microsoft.maui!Microsoft.Maui.WeakEventManager.AddEventHandler(System.EventHandler
1<TEventArgs_REF>,string)`microsoft.maui!Microsoft.Maui.CommandMapper.GetCommand(string)
Which show up as a mixture of
string
comparers:In cases of
Dictionary<string, TValue>
orHashSet<string>
, we can useStringComparer.Ordinal
for faster dictionary lookups.Unfortunately, there is no code analyzer for this:
dotnet/runtime#52399
So, I manually went through the codebase and found all the places.
I now only see the fast string comparers in this sample:
Which is about ~0.36% better than before.
This should slightly improve the performance of handlers & all controls on all platforms.
I also fixed
Microsoft.Maui.Graphics.Text.TextColors
to useStringComparer.OrdinalIgnoreCase
-- and removed aToUpperInvariant()
call.