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

Suboptimal code for matching nullable value to constant pattern #42912

Open
gafter opened this issue Mar 30, 2020 · 1 comment
Open

Suboptimal code for matching nullable value to constant pattern #42912

gafter opened this issue Mar 30, 2020 · 1 comment
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code Feature Request help wanted The issue is "up for grabs" - add a comment if you are interested in working on it New Language Feature - Pattern Matching Pattern Matching
Milestone

Comments

@gafter
Copy link
Member

gafter commented Mar 30, 2020

See https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAdYwggdsgZgB8ABARgDYACEgJhrIHYBYAKAG82ruaCHrgAe0EAbKgElkAQQDG8KCIAUAS1wwA/FQAeASioBeAHzaqy5FTIBuKlx4k+5AcLGSAolvQQ5EACYq1mroGxloG+haWbAC+bGxAA=

This issue is extracted from #31494

For source

static class Program
{
    public static bool IsActual(int? x) => x is 1; 
    public static bool IsExpected(int? x) => x == 1;
}

The generated code is

internal static class Program
{
    public static bool IsActual(int? x)
    {
        if (x.HasValue)
        {
            return x.GetValueOrDefault() == 1;
        }
        return false;
    }

    public static bool IsExpected(int? x)
    {
        int? num = x;
        int num2 = 1;
        return (num.GetValueOrDefault() == num2) & num.HasValue;
    }
}

The point is that we optimized == to avoid control-flow. We should do the same for the pattern-matching operations.

@gafter gafter added Area-Compilers Feature Request Code Gen Quality Room for improvement in the quality of the compiler's generated code New Language Feature - Pattern Matching Pattern Matching labels Mar 30, 2020
@gafter gafter added this to the Backlog milestone Mar 30, 2020
@gafter gafter added this to Code Quality in Compiler: Pattern-Matching Mar 30, 2020
@RikkiGibson RikkiGibson added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Sep 10, 2020
@sharwell
Copy link
Member

Here's another case:

public bool IsDefaultOrEmpty => _dictionary?.Count is null or 0;

Expected:

int? num = _dictionary?.Count;
return num.GetValueOrDefault() == 0;

Actual:

int? num = _dictionary?.Count;
return !num.HasValue || num.GetValueOrDefault() == 0;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code Feature Request help wanted The issue is "up for grabs" - add a comment if you are interested in working on it New Language Feature - Pattern Matching Pattern Matching
Projects
No open projects
Development

No branches or pull requests

3 participants