Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline expansion in C#

I am working on a math library for my DirectX 3D engine in C#. I am using SlimDX, which is a wonderfuly put together and powerful library. SlimDX provides quite a few math classes, but they are actually wrappers around native D3DX objects, so while the objects themselves are very, very fast, the interop is not I presume, because my managed C# code outperforms them.

My specific question has to do with floating-point comparison. The sort-of canonical way is to define an epsilon value, and compare the value to the difference between the floating-point values to determine closeness, like so:

float Epsilon = 1.0e-6f;
bool FloatEq(float a, float b)
{
  return Math.Abs(a - b) < Epsilon
}

The function call overhead would swamp the actual comparison, so this trivial function would be inlined in C++, will the C# compiler do this? Is there a way to tell the C# compiler that I want a method inlined?

like image 794
Chris D. Avatar asked Jul 13 '10 11:07

Chris D.


1 Answers

The C# compiler doesn't perform inlining - but the JIT may well do so in this case. That's certainly the piece which would perform inlining, if anything.

EDIT: I would expect it to inline in this case, but it's hard to tell without loading the code into something like cordbg with JIT optimizations turned on. Each different CLR version may well have different tweaks to its inlining (including the 64 bit vs 32 bit CLRs) so I'm reluctant to say anything too definite.

like image 107
Jon Skeet Avatar answered Sep 28 '22 22:09

Jon Skeet