A simple method with a pattern match is generates less efficient code starting from .NET 8 compared to .NET 6/7.
Repro: godbolt
public static bool HasLeadingSlash(ReadOnlySpan<char> path)
{
if (path.Length > 0 && path[0] is '/' or '\\')
return true;
return false;
}
.NET 8,9,10 (IL size: 49 bytes):
public static bool HasLeadingSlash(ReadOnlySpan<char> path)
{
bool flag = path.Length > 0;
if (flag)
{
char c = path[0];
bool flag2 = ((c == '/' || c == '\\') ? true : false);
flag = flag2;
}
if (flag)
{
return true;
}
return false;
}
.NET 6,7 (IL size: 34 bytes):
public static bool HasLeadingSlash(ReadOnlySpan<char> path)
{
if (path.Length > 0)
{
char c = path[0];
if (c == '/' || c == '\\')
{
return true;
}
}
return false;
}
Roslyn now introduces unnecessary temporary bool variables and extra conditional branches. The JIT is unable to untangle this pattern, resulting in larger, slower code.
A simple method with a pattern match
isgenerates less efficient code starting from.NET 8compared to.NET 6/7.Repro: godbolt
.NET 8,9,10 (IL size: 49 bytes):
.NET 6,7 (IL size: 34 bytes):
Roslyn now introduces unnecessary temporary
boolvariables and extra conditional branches. The JIT is unable to untangle this pattern, resulting in larger, slower code.