dotnet/roslyn

Codegen of System.Nullable `is null` check is poor

Open

#65,091 opened on Oct 28, 2022

View on GitHub
 (2 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
4 - In ReviewArea-CompilersCode Gen QualityConcept-Design Debthelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

Codegen of System.Nullable is null check is poor

Take a look at: https://sharplab.io/#v2:C4LglgNgPgAgLAAgAoHsUCcDCKAmBTAczwDsAKMYhC4AfgQDcBKAWACgBvNhbqgMwVL0qAZwTEArhAiMEXHvJgB2ANxsAvmzbwEAcTQ5s+ImQpVitBiw5zuYfqQCE9AHQAJAIbCAau4ji8MjbyCEqqrBqsWoh6uIaEJABM5JTUdExsnKzydgJCALx5YpLSslnB3KHqQA

void PoorCodegen(in int? v)
{
    if (v is null) 
        return;
}

void GoodCodegen(in int? v)
{
    if (!v.HasValue) 
        return;
}

void GoodCodegen2(in int? v)
{
    if (v == null) 
        return;
}

In the pattern matching case there suddenly is a copy.

Contributor guide