runtime: Inline array Equals unexpected behavior
Description
Default Equals implementation compares only the first element of inline array.
Reproduction Steps
var a1 = new A();
a1[0]=1;
a1[1]=2;
var a2 = new A();
a2[0] = 1;
a2[1] = 1;
Console.WriteLine(a1.Equals(a2));
[InlineArray(2)]
struct A
{
public int? P;
}
This code prints true sharplab
Expected behavior
Should compare all values in inline array and print false in this case.
Actual behavior
Compares only the first element.
Regression?
No
Known Workarounds
Override Equals
Configuration
.net 8, Win 10
Other information
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 20 (16 by maintainers)
It does not. Run the following:
It prints true. It compares the
Fieldonly. It does not compare the remaining 36 bytes. There is no obvious right way to compare the remaining 36 bytes. We do not know what’s actually stored in there bytes.Yep. Here is the updated doc PR if you’d like to see how we are documenting this - https://github.com/dotnet/dotnet-api-docs/pull/10036
Looks like we should be extremely careful when using
InlineArrayand read the manual before using 😃But you can’t use spans as dictionary key or box it unlike inline array and it does not violate
objectcontract.@colejohnson66 It is a fair opinion, but not an obviously “correct” one.
Your last statement, above, is at least part of the reasoning. Providing a default for an equality value check has been contentious for a long time and one that we would likely not make again.
Aside from the opinions regarding default equality, there are other considerations too. Specifically, common uses of the
InlineArrayAttributefeature, inherent complexity in what many consider the correct default implementation, and finally performance of the default implementation.A common use case for
InlineArrayAttributeis as a scratch or temporary buffer that may be partially set. This means that simply doing a naive bitwise check can be incorrect. It is reasonable to say the default of comparing the entire buffer is correct, but this leads into the second consideration, complexity. If theInlineArrayAttributeis a set ofobjectinstances, we can’t perform a simple bitwise check, but instead must iterate over each of them, check fornull, and then callEquals(). All this can be done withSpan<object>so this isn’t a technical issue, but rather one of complexity. Finally, let’s consider the performance angle. The default implementation uses reflection and so is, by default, slow and not AOT friendly. None of this push back is technical, but rather a debate about what is correct, intuitive, and performant, and if it is worth investing in a default implementation. In the opinion of people on the runtime team, it isn’t.User defined
ValueTypes that expect to have theirEquals()orGetHashCode()called, should be overridden by the user.Yes, I think we should.
System.ValueTypehas a default behavior that is done by the runtime where effectively calls Equals on each field. I expect it wasn’t updated to considerInlineArrayat all, potentially by design.