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
Tiered compilation should generate optimized overrides for ValueType.GetHashCode and ValueType.Equals #36613
Comments
it should always be overridden if used. Your S1 and S2 implementations are not the same, e.g. int hc1 = new S1 { s = "Hello", i = 10 }.GetHashCode();
int hc2 = new S1 { s = "Hello", i = 12 }.GetHashCode();
|
What is the reason not to use all the fields in GetHashCode? |
runtime/src/coreclr/src/vm/comutilnative.cpp Line 2032 in 4224c4c
|
So it would theoretically be possible to make both changes in one go, and use all the fields for GetHashCode, and then optimize that on the fly when it seems necessary. |
Maybe? I personally agree with #9802 (comment) |
The jit can't generate overrides like this; that has to be done when the method table for the type is created. Changing this to area-vm. |
Related: #4414 |
It's well known that if a valuetype is used as a dictionary or hashset key, or in any other situation where
.Equals
or.GetHashCode
is often used, you should override the defaultValueType.Equals
and.GetHashCode
as the default implementation is reflection based and very slow.For example here is a benchmark comparing a struct using the
Equals
andGetHashCode
roslyn generates to one without any override:However the override is functionally identical to the default one, and so it seems pointless to generate this for every struct used as a dictionary key. It's also too easy to forget to do so for a struct which needs it, or alternatively do it for too many structs and lead to code bloat.
Is it possible for the Jit to generate the same override at runtime? Whilst it would not be practical to do so for every struct, tiered compilation might be able tell you which structs have
Equals
andGetHashCode
used in hot paths.The text was updated successfully, but these errors were encountered: