-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Do not call GetHashCode() on enums #5639
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
Conversation
|
Closing to reopen and trigger a CI build. See #5646. |
| 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); |
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.
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?
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.
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.
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.
|
Thanks! |

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.