Avoid boxing when structs are used as collection keys.#69443
Merged
ToddGrun merged 1 commit intodotnet:mainfrom Aug 10, 2023
Merged
Conversation
Change several structs to record structs such that they no longer are boxed when used as keys in MultiDictionary's internal ImmutableHashSet. When MultiDictionary's value is a struct and is created without specifying the value's IEqualityComparer, the default struct comparer is used. This equality comparer is notoriously slow due to boxing. By switching these structs to record structs, an implementation of IEquatable is generated and the default struct equality comparer isn't used. Addresses dotnet#69165 Sam mentioned that there is an issue already logged in the roslyn-analyzers repo around adding an analyzer to detect this. dotnet/roslyn-analyzers#1640
sharwell
reviewed
Aug 9, 2023
|
|
||
| [DataContract] | ||
| internal readonly struct HighlightSpan | ||
| internal readonly record struct HighlightSpan |
Contributor
There was a problem hiding this comment.
❓ Why is this one changing?
Contributor
There was a problem hiding this comment.
➡️ This type isn't used as a dictionary key, but rather as a dictionary value in a type that happens to create a set from the values.
CyrusNajmabadi
approved these changes
Aug 9, 2023
sharwell
reviewed
Aug 9, 2023
| } | ||
|
|
||
| private readonly struct ParameterTypeInfo(string name, bool isComplex, bool isArray) | ||
| private readonly record struct ParameterTypeInfo(string name, bool isComplex, bool isArray) |
Contributor
There was a problem hiding this comment.
❓ Why is this one changing?
Contributor
There was a problem hiding this comment.
➡️ This type isn't used as a dictionary key, but rather as a dictionary value in a type that happens to create a set from the values.
genlu
approved these changes
Aug 10, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Change several structs to record structs such that they no longer are boxed when used as keys in MultiDictionary's internal ImmutableHashSet. When MultiDictionary's value is a struct and is created without specifying the value's IEqualityComparer, the default struct comparer is used. This equality comparer is notoriously slow due to boxing. By switching these structs to record structs, an implementation of IEquatable is generated and the default struct equality comparer isn't used.
Addresses #69165
Sam mentioned that there is an issue already logged in the roslyn-analyzers repo around adding an analyzer to detect this.
dotnet/roslyn-analyzers#1640