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)

Most upvoted comments

It looks like the auto-generated equality for that struct does a bitwise-compare.

It does not. Run the following:

using System.Runtime.InteropServices;

unsafe
{
    Buffer b1 = new Buffer();
    *((int*)&b1 + 1) = 2;
    Buffer b2 = new Buffer();
    *((int*)&b2 + 1) = 3;

    Console.WriteLine(b1.Equals(b2));
}

[StructLayout(LayoutKind.Sequential, Size = 40)]
public struct Buffer
{
   public int Field;
}

It prints true. It compares the Field only. 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.

Looks like we should be extremely careful when using InlineArray and read the manual before using 😃

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 InlineArray and 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 object contract.

@colejohnson66 It is a fair opinion, but not an obviously “correct” one.

Forcing users to write it themselves is the same logic as saying ValueType.Equals shouldn’t exist: “just have the user override Object.Equals themselves!”

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 InlineArrayAttribute feature, inherent complexity in what many consider the correct default implementation, and finally performance of the default implementation.

A common use case for InlineArrayAttribute is 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 the InlineArrayAttribute is a set of object instances, we can’t perform a simple bitwise check, but instead must iterate over each of them, check for null, and then call Equals(). All this can be done with Span<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 their Equals() or GetHashCode() called, should be overridden by the user.

Should we throw from the default implementation for InlineArrays? (It would be a breaking change.)

Yes, I think we should.

System.ValueType has a default behavior that is done by the runtime where effectively calls Equals on each field. I expect it wasn’t updated to consider InlineArray at all, potentially by design.