.NET 7 increment operator ++ and +1 compiling differently

Since they are equivalent ++ and +1 should compile to the same code, right? As we can see below, this isn’t always the case.

In this case the performance difference is tiny, but for cases with large structs, loops, etc., it could be significant.

We can see from SharpLab below that +=1 and the decrement operator approach also gets converted to inefficient JIT ASM compared to +1

Apparently this issue is fixed in .NET 8 Preview 1 which should be available in a few months.

Here’s the GitHub issue for the fix -> JIT: Clone all local addresses when importing `dup` by jakobbotsch ¬∑ Pull Request #72714 ¬∑ dotnet/runtime (github.com)

Thanks to Bartosz Adamczewski for creating the above (first image on page) awesome infographic.

Leave a Reply

Your email address will not be published. Required fields are marked *