Skip to content

Conversation

MichalStrehovsky
Copy link
Member

@MichalStrehovsky MichalStrehovsky commented Aug 22, 2025

This is same as #111620 but with an extra thing: enable unconditional method body folding on __GetFieldHelper overrides (both the simplified ones that just return a negative number and normal ones).

Before this PR we considered valuetypes as byte-hashable/comparable only if the valuetype didn't have any trailing padding.

This fixes things up so that we can consider types with trailing padding byte-hashable/comparable if the other requirements still hold (such as no double/float fields, no GC references, etc.) and there is a trailing padding. In this case, we remember the number of bytes that we should be looking at and ignore the rest.

Since this doesn't seem to provide meaningful size savings according to rt-sz, this is a throughput improvement, not a size improvement.

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static object Get() => default(Helper);

    static void Main()
    {
        for (int i = 0; i < 10; i++)
        {
            var s = Stopwatch.StartNew();

            object o = Get();
            int acc = 0;

            for (int j = 0; j < 10000000; j++)
            {
                acc += o.GetHashCode();
            }

            Console.WriteLine($"{s.ElapsedMilliseconds} ({acc})");
        }
    }

    struct Helper
    {
        long _one;
        byte _two;
        byte _three;
        byte _four;
        byte _five;
    }
}

Before:

243 (-899317760)
243 (-899317760)
246 (-899317760)
244 (-899317760)
244 (-899317760)
245 (-899317760)
240 (-899317760)
240 (-899317760)
232 (-899317760)
248 (-899317760)

After:

229 (565863296)
229 (565863296)
228 (565863296)
229 (565863296)
233 (565863296)
228 (565863296)
229 (565863296)
230 (565863296)
228 (565863296)
230 (565863296)

Resolves #111542

Cc @dotnet/ilc-contrib

This is same as dotnet#111620 but with an extra thing: enable unconditional method body folding on `__GetFieldHelper` overrides (both the simplified ones that just return a negative number and normal ones).
@Copilot Copilot AI review requested due to automatic review settings August 22, 2025 10:34
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves performance of value type hashing and equality comparisons by enabling fast hashing helpers for types with trailing padding. The changes allow value types to use memory-based comparisons up to a specified offset rather than requiring field-by-field comparisons when there's trailing padding.

Key changes:

  • Modified the compiler to enable method body folding for __GetFieldHelper overrides unconditionally
  • Enhanced the fast path logic to handle value types with trailing padding by comparing memory up to the last field's end offset
  • Updated the API semantics to use negative return values to indicate the byte offset for memory comparison

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
ObjectDataInterner.cs Enables unconditional method body folding for ValueTypeGetFieldHelperMethodOverride methods
ValueTypeGetFieldHelperMethodOverride.cs Adds fast path for value types that can use memory comparison up to a specific offset
ComparerIntrinsics.cs Extends the analysis to support partial memory comparison up to the last field's end offset
ValueType.cs Updates the runtime implementation to handle negative return values as byte offsets for memory comparison

Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky MichalStrehovsky marked this pull request as draft August 22, 2025 10:36
@MichalStrehovsky MichalStrehovsky marked this pull request as ready for review August 26, 2025 10:25
@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
Copy link
Member Author

/ba-g the arm32 failures are known and have a fix out

@MichalStrehovsky MichalStrehovsky merged commit 5cb2a05 into dotnet:main Aug 27, 2025
117 of 121 checks passed
@MichalStrehovsky MichalStrehovsky deleted the fix111542 branch August 27, 2025 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[NativeAOT] __GetFieldHelper emitted for simple contiguous struct
2 participants