Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic type check allocates for nullable structs #95685

Closed
kindermannhubert opened this issue Dec 6, 2023 · 3 comments · Fixed by #95711
Closed

Generic type check allocates for nullable structs #95685

kindermannhubert opened this issue Dec 6, 2023 · 3 comments · Fixed by #95711
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Milestone

Comments

@kindermannhubert
Copy link

Description

The following two methods (M1, M2) should behave identically:

public static bool M1<T>(T value) => value is S;

public static bool M2<T>(T value)
{
    if (typeof(T) == typeof(S))
    {
        return true;
    }
    else if (typeof(T) == typeof(S?))
    {
        return Unsafe.As<T, S?>(ref value).HasValue;
    }
    return false;
}

However, to my surprise, M1 seems to allocate when T is a nullable struct.

Here's SharpLab's link: https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAQEwEYCwAUJgAwAEAygBYQBOADgDITAB0ASgK4wJRgCmAbiKkymXB268BrAMIB7MPSgAbfrQrqAblADG/FMMJEUCWp10JKZAN4BfEdjKyyRG0TKeyAbQBSUBABxfhh1PQAKBABPen55ADNwigBKZIBdDy8/AODQ2gjo2ITw2ARUjMIvH38gkLDdSJi4xIoAfnLMz2zavIKm4tL29M6xAGYxXAA2MmB5eRUyAFlcAB4AFQA+cLWyLQgVTn5ksgBeDd39w7IoFEojEe7c+sailo7KrJqn/IbC5pKeO8qo86j8Xv82kDPjlQX1XgCEEMKlVMONxNNZvMlth1lsdnsDkcRu4PlVPFB4mRwcU1scTicyH9iilkiMqiSyZyxAB2RnmIRsrwOUlk/gqFD8a6U6mJWmnBlMlpDQWeDlclG8gCqMBQEHi/FYAEEUOs4JRWltaPxKQTDslWAAJVAANUuApFVWF6swvPi+wlRk9RDsQA

When T is a non-nullable struct, the JIT is smart enough to know the result beforehand.
When T is a nullable struct the resulting code calls System.Runtime.CompilerServices.RuntimeHelpers.Box(System.Runtime.CompilerServices.MethodTable*, Byte ByRef).

To confirm, I profiled the following code:

long x = 0;
S? value = new S();
for (long i = 0; i < long.MaxValue - 1; i++)
{
    x += M1(value) ? 0 : 1;
}
Console.WriteLine(x);

I used Visual Studio with .NET Object Allocation Tracking, and it confirmed that M1 allocates while M2 does not.

These observations also hold for a slightly different version (from which I came) but I suppose it has the same underlying issue:

interface I { }
struct S : I { }

static bool M1<T>(T value) => value is I;

I believe this simple code definitely should not allocate.

Configuration

Windows 10, .NET 7 and .NET 8.

@kindermannhubert kindermannhubert added the tenet-performance Performance related issue label Dec 6, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Dec 6, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 6, 2023
@ghost
Copy link

ghost commented Dec 6, 2023

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

Issue Details

Description

The following two methods (M1, M2) should behave identically:

public static bool M1<T>(T value) => value is S;

public static bool M2<T>(T value)
{
    if (typeof(T) == typeof(S))
    {
        return true;
    }
    else if (typeof(T) == typeof(S?))
    {
        return Unsafe.As<T, S?>(ref value).HasValue;
    }
    return false;
}

However, to my surprise, M1 seems to allocate when T is a nullable struct.

Here's SharpLab's link: https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAQEwEYCwAUJgAwAEAygBYQBOADgDITAB0ASgK4wJRgCmAbiKkymXB268BrAMIB7MPSgAbfrQrqAblADG/FMMJEUCWp10JKZAN4BfEdjKyyRG0TKeyAbQBSUBABxfhh1PQAKBABPen55ADNwigBKZIBdDy8/AODQ2gjo2ITw2ARUjMIvH38gkLDdSJi4xIoAfnLMz2zavIKm4tL29M6xAGYxXAA2MmB5eRUyAFlcAB4AFQA+cLWyLQgVTn5ksgBeDd39w7IoFEojEe7c+sailo7KrJqn/IbC5pKeO8qo86j8Xv82kDPjlQX1XgCEEMKlVMONxNNZvMlth1lsdnsDkcRu4PlVPFB4mRwcU1scTicyH9iilkiMqiSyZyxAB2RnmIRsrwOUlk/gqFD8a6U6mJWmnBlMlpDQWeDlclG8gCqMBQEHi/FYAEEUOs4JRWltaPxKQTDslWAAJVAANUuApFVWF6swvPi+wlRk9RDsQA

When T is a non-nullable struct, the JIT is smart enough to know the result beforehand.
When T is a nullable struct the resulting code calls System.Runtime.CompilerServices.RuntimeHelpers.Box(System.Runtime.CompilerServices.MethodTable*, Byte ByRef).

To confirm, I profiled the following code:

long x = 0;
S? value = new S();
for (long i = 0; i < long.MaxValue - 1; i++)
{
    x += M1(value) ? 0 : 1;
}
Console.WriteLine(x);

I used Visual Studio with .NET Object Allocation Tracking, and it confirmed that M1 allocates while M2 does not.

These observations also hold for a slightly different version (from which I came) but I suppose it has the same underlying issue:

interface I { }
struct S : I { }

static bool M1<T>(T value) => value is I;

I believe this simple code definitely should not allocate.

Configuration

Windows 10, .NET 7 and .NET 8.

Author: kindermannhubert
Assignees: -
Labels:

tenet-performance, area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Dec 6, 2023

Basically, impBoxPatternMatch needs to learn box+isinst+ldnull+cmp pattern (as was mentioned in the issue ^)

@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Dec 6, 2023
@EgorBo EgorBo added this to the Future milestone Dec 6, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Dec 7, 2023
@EgorBo EgorBo modified the milestones: Future, 9.0.0 Dec 7, 2023
@EgorBo EgorBo self-assigned this Dec 7, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Dec 7, 2023
@EgorBo
Copy link
Member

EgorBo commented Dec 7, 2023

Thanks for reporting! Fixed in .NET 9.0

@github-actions github-actions bot locked and limited conversation to collaborators Jan 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants