Do not call GetHashCode() on enums#5639
Merged
Forgind merged 2 commits intodotnet:masterfrom Aug 14, 2020
Merged
Conversation
Member
|
Closing to reopen and trigger a CI build. See #5646. |
Forgind
reviewed
Aug 10, 2020
| hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(obj.ElementName); | ||
| hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(obj.ElementDescription); | ||
| hashCode = hashCode * -1521134295 + obj.Kind.GetHashCode(); | ||
| hashCode = hashCode * -1521134295 + EqualityComparer<EvaluationLocationKind>.Default.GetHashCode(obj.Kind); |
Contributor
There was a problem hiding this comment.
I wasn't familiar with this idiom, and when I tried putting it into sharplab, it seemed to do the same thing. What did I do wrong?
Member
There was a problem hiding this comment.
Member
Author
There was a problem hiding this comment.
One super hacky way to quickly check if a piece of code allocates is simply running it in a tight while (true) loop in a trivial console app and checking the VS diagnostics window.
Seeing small yellow markers appear regularly means that GC is collecting something and that something has to be allocations performed in the loop.
Forgind
approved these changes
Aug 10, 2020
Contributor
|
Thanks! |
benvillalobos
approved these changes
Aug 12, 2020
cdmihai
approved these changes
Aug 13, 2020
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.

Calling
GetHashCode()on an enum results in boxing (i.e. a GC allocation) when running on .NET Framework. This has been addressed in .NET Core (dotnet/coreclr#7895) but we want to fix it on our side still because it's showing in Visual Studio profiles:https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1166715
This PR is also removing the calls to
base.GetHashCode()which are pointless in structures that calculate the hashcode out of all their fields.