Skip to content

Conversation

@ladipro
Copy link
Member

@ladipro ladipro commented Aug 10, 2020

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.

@benvillalobos
Copy link
Member

benvillalobos commented Aug 10, 2020

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);
Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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.

image

@Forgind
Copy link
Contributor

Forgind commented Aug 10, 2020

Thanks!

@Forgind Forgind added the merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now. label Aug 14, 2020
@Forgind Forgind merged commit 8cdfa6d into dotnet:master Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-when-branch-open PRs that are approved, except that there is a problem that means we are not merging stuff right now.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants