Open Youssef1313 opened 4 months ago
Using ObjectEqualityComparer will cause boxing for every equality operation being done.
I think it's rather an unfortunate RyuJIT's limitation (mainly, https://github.com/dotnet/runtime/issues/9118) and should be fixed in future, so should we introduce temporarily analyzers?
What I guess makes sense to do is to warn if a struct doesn't override Equals at all probably (if it's not already a warning/suggestion).
Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.
Background and motivation
Using records in C# can be tricky (performance-wise) if the developer isn't aware of some implementation details. The compiler generates the equality implementation using
EqualityComparer<T>.Default
, which isObjectEqualityComparer
for a struct that is notIEquatable<T>
.Using
ObjectEqualityComparer
will cause boxing for every equality operation being done.API Proposal
An analyzer that warns if a record primary constructor has a parameter which is a value type (struct) that doesn't implement
IEquatable<T>
whereT
is the same value type.For example:
API Usage
Alternative Designs
No response
Risks
No response