Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using unchecked context hurt performance or portability in C#?

I want to implement a fast hash function that will use int datatype and rely on integer overflow. MSDN says that in order to guarantee that overflows don't trigger exceptions I have to use unchecked blocks for that code.

Suppose I surround only that computation in an unchecked block. Will my code have any performance or portability problems because of that?

like image 313
sharptooth Avatar asked Oct 20 '11 08:10

sharptooth


3 Answers

checked add only one process instruction:

checked
{
    int y = x * x;
05297978  mov         eax,dword ptr [ebp-10h]  
0529797B  imul        eax,dword ptr [ebp-10h]  
0529797F  jno         05297986  //if not overflow: goto 05297986
05297981  call        72A29522  //invoke exception
05297986  mov         dword ptr [ebp-14h],eax  
}

unchecked
{
    int yy = xx * xx;
0529799E  mov         eax,dword ptr [ebp-18h]  
052979A1  imul        eax,dword ptr [ebp-18h]  
052979A5  mov         dword ptr [ebp-1Ch],eax  
}
like image 170
Glebka Avatar answered Nov 04 '22 09:11

Glebka


Technically only the checked blocks should slow. So I don't think an unchecked block (a block where the framework has to do less checks) could slow. It isn't a context switch or something similar. The JIT simply doesn't emit instructions to check for overflow/underflow. Now, clearly if someone created a "special" processor where overflow must be simulated and ported Mono on it, or where an overflow causes different results than on Intel processors, an unchecked block would be slower (because the JIT would have to "simulate" it). But note that the base types of .NET are defined in the ECMA standard. Signed ints must be based on two-complement for example, and their size must be 8, 16, 32, 64 bits. There isn't much space for "strange" processors that use 36 bits integers.

like image 26
xanatos Avatar answered Nov 04 '22 08:11

xanatos


As a rule you can expect unchecked arithmetic to be slightly faster, but it's pretty much never worth asking the opposite question to yours (viz. "will using checked hurt my performance?").

Checked and unchecked just mean there are slightly different rules as to how operators like +, *, and - are treated, and you should use the one appropriate for the case in hand.

In this case, you quite definitely want unchecked, so you should state this in your code. This actually increases portability, since you will then have the same behaviour whatever compiler switches are used with it.

like image 30
Jon Hanna Avatar answered Nov 04 '22 09:11

Jon Hanna