Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# jitter improvements in future framework versions

I noticed that C# jitter produces measurably slower code than C++ compiler, even if there are no "managed overhead" constructs used (such as arrays with checked indexing).

To quantify it, I timed the following simple loop:

public static int count = 1000000000;
public static int Main()
{
    int j = 0;
    for (int i = 0; i < count; ++i)
    {
        j += (i % 2 == 0) ? ((i + 7) >> 3) : (i * 7);
    }
    return j;
}

This loop takes 3.88s to execute (compiled with /o). Equivalent loop compiled with VC 2010 (-O2) takes 2.95s.

To verify that inferior code was actually generated I compared machine codes: created a listing (/FAs) from VC compiler, and attached a debugger to C# program (after the loop was completed).

Indeed, C++ version is using some clever tricks. For example to avoid costly multiplication by 7, there is a separate register that is incremented by 7 every loop count. C# version does the multiplication (imul) every time. There are other differences as well.

I understand that C# jitter has a lot less time to compile the code at runtime than VC at build time. But e.g. Java jitter is dynamically optimizing frequently used methods. C# doesn't seem to be doing it.

My question is: are there plans to improve C# jitter in the future framework versions?

like image 405
kaalus Avatar asked Aug 16 '12 14:08

kaalus


1 Answers

Release builds, VS2008SP1, .NET 3.5SP1, average of 10 tests:

.NET, x86:   2.646 seconds
C++, x86:    2.652 seconds
.NET, x64:   2.352 seconds
C++, x64:    2.090 seconds

Classic mistakes are assuming that /o is significant, measuring jitting time, running the debug build, testing with the debugger attached so the jitter optimizer is disabled.

The x64 jitter uses the same trick you mentioned, it isn't unique to the C++ code generator:

00000030  xor         r9d,r9d 
... 
00000059  add         r9d,7

A new feature for .NET 4.5 is Profile Guided Optimization.

Future plans are never shared by a company like Microsoft and there is no point in guessing at them.

like image 60
Hans Passant Avatar answered Sep 23 '22 02:09

Hans Passant