https://godbolt.org/z/aYMq87E9G
// Simplified repro
public static int N(ReadOnlySpan<char> s, int i)
{
while (true)
{
if ((uint)++i >= (uint)s.Length)
break;
if (s[i] == '?')
return i;
}
return -1;
}
Generated ASM:
lea eax, [rdx+0x01]
mov edx, eax ; redundant
cmp edx, esi
With a separate increment:
++i;
if ((uint)i >= (uint)s.Length)
break;
Generated ASM:
.NET 8.0 behavior: both variants generate identical inc; cmp code.
Issue:
- Inline
++i adds an extra mov, increasing register pressure
- Causes spills in register-constrained methods
- Larger binary size
https://godbolt.org/z/aYMq87E9G
Generated ASM:
With a separate increment:
Generated ASM:
.NET 8.0 behavior: both variants generate identical
inc;cmpcode.Issue:
++iadds an extramov, increasing register pressure