Skip to content

Avoid boxing ArgumentNullException.ThrowIfNull for user-defined struct arguments in Tier-0 #128574

Closed

Description

@PetrAltman

Description

PR #104815 (fixing #104512) added a pattern match in impBoxPatternMatch that eliminates boxing of ArgumentNullException.ThrowIfNull arguments in Tier-0.

The optimization fires for primitives (int, long, etc.) but not for user-defined structs — the box allocation persists indefinitely in Tier-0 and only disappears in Tier-1 when the entire call chain inlines into a hot loop and escape analysis removes the dead box.

This affects every BCL API that calls ArgumentNullException.ThrowIfNull(key) on a generic TKey (e.g. SortedList<TKey, TValue>.IndexOfKey — the original motivation in #100406) whenever TKey is a user-defined struct.

Reproduction

using System.Runtime.CompilerServices;

Test();

[MethodImpl(MethodImplOptions.NoInlining)]
static void Test()
{
    for (int it = 0; it < 20; it++)
    {
        var a0 = GC.GetAllocatedBytesForCurrentThread();
        for (int i = 0; i < 1000; i++) Call(new MyKey(i));
        var a1 = GC.GetAllocatedBytesForCurrentThread();
        Console.WriteLine($"iter {it}: {a1 - a0} B");
    }
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Call(MyKey k) => ArgumentNullException.ThrowIfNull(k);

readonly struct MyKey
{
    public readonly int Value;
    public MyKey(int value) => Value = value;
}

Expected

0 B per iteration — the boxed reference is provably non-null and never escapes ThrowIfNull, so the box can be folded away regardless of the concrete value type.

This is what happens when MyKey is replaced with int (PR #104815 baseline).

Actual

iter 0:  24000 B
iter 1:  24000 B
...
iter 19: 24000 B

24 B per call (= one boxed MyKey), forever. Removing [NoInlining] from Call makes the allocation disappear after Tier-1 promotion around iteration 10, but only because the wrapper then inlines and the dead box is removed by general optimizations — not by impBoxPatternMatch itself.

Environment

  • .NET SDK 10.0.200, TFM net10.0, Release, win-x64
  • Default runtime config (Tiered Compilation + QuickJit + QuickJitForLoops + PGO)

Related

Activity

  1. added
    tenet-performance
    on May 26, 2026
  2. added
    untriaged
    on May 26, 2026
  3. added
    area-CodeGen-coreclr
    on May 26, 2026
  4. dotnet-policy-service commented on May 26, 2026

    @dotnet-policy-service
    Contributor

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

  5. removed
    untriaged
    on May 26, 2026
  6. added this to the 11.0.0 milestone on May 26, 2026
  7. added 2 commits that reference this issue on Jun 14, 2026
    f7dc900
    3d40d73
  8. added a commit that references this issue on Jul 15, 2026
    9155afc
  9. locked and limited conversation to collaborators on Jul 16, 2026
  10. added a commit that references this issue on Jul 22, 2026
    2f58e0c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions