Skip to content

Optimize is pattern when comparands are constant #60534

@gfoidl

Description

@gfoidl

E.g. for

static bool M1(int x) => x is < 100 or > 511;

code similar to

if (x >= 100)
{
    return x > 511;
}
return true;

is generated. This could be optimized to

return (uint)(x - 100) > 411u;

which should be faster and less machine code (as well IL code) on all platforms, by avoiding an extra comparison and jump.

JIT could do this too, but JIT is a live-compiler (also applies to tiered compilation), and Roslyn is recognizing the is pattern anyway.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions