简体   繁体   English

C#中的内联扩展

[英]Inline expansion in C#

I am working on a math library for my DirectX 3D engine in C#. 我正在为C#中的DirectX 3D引擎开发数学库。 I am using SlimDX, which is a wonderfuly put together and powerful library. 我正在使用SlimDX,这是一个奇妙的拼凑和强大的库。 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. SlimDX提供了相当多的数学类,但它们实际上是本机D3DX对象的包装器,所以虽然对象本身非常非常快,但我认为互操作不是因为我的托管C#代码优于它们。

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: 排序规范的方法是定义epsilon值,并将值与浮点值之间的差值进行比较以确定接近度,如下所示:

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? 函数调用开销会破坏实际的比较,所以这个简单的函数将在C ++中内联,C#编译器会这样做吗? Is there a way to tell the C# compiler that I want a method inlined? 有没有办法告诉C#编译器我想要一个内联方法?

The C# compiler doesn't perform inlining - but the JIT may well do so in this case. C#编译器不执行内联 - 但在这种情况下JIT可能会这样做。 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. 编辑:在这种情况下,我希望它能内联,但是如果没有将代码加载到像打开JIT优化的cordbg这样的代码中,很难说。 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. 每个不同的CLR版本可能会对其内联进行不同的调整(包括64位与32位CLR),所以我不愿意说太明确。

Here's some info about the circumstances under which the JIT won't perform inlining; 这里有一些关于JIT不会进行内联的情况的信息;

http://blogs.msdn.com/b/davidnotario/archive/2004/11/01/250398.aspx http://blogs.msdn.com/b/davidnotario/archive/2004/11/01/250398.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM