dotnet/roslyn
Suboptimal code for matching nullable value to constant pattern
Ouverte
#42 912 ouverte le 30 mars 2020
Area-CompilersCode Gen QualityFeature - Pattern MatchingFeature Requesthelp wanted
Métriques du dépôt
- Stars
- (20 414 étoiles)
- Métriques de merge PR
- (Merge moyen 6j 17h) (256 PRs mergées en 30 j)
Description
This issue is extracted from https://github.com/dotnet/roslyn/issues/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.