内联函数
什么是内联?
它是干什么用的?
你能在 C# 中内联一些东西吗?
What is inlining?
What is it used for?
Can you inline something in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
什么是内联?
它是干什么用的?
你能在 C# 中内联一些东西吗?
What is inlining?
What is it used for?
Can you inline something in C#?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
它是什么
在 C 和 C++ 术语中,您使用 inline 关键字告诉编译器调用例程,而无需将参数推入堆栈的开销。 相反,函数将其机器代码插入到调用它的函数中。 这可以在某些情况下显着提高性能。
危险
随着内联函数大小的增加,使用“内联”的速度优势会显着降低。 过度使用实际上会导致程序运行速度变慢。 内联一个非常小的访问器函数通常会减少代码大小,而内联一个非常大的函数会显着增加代码大小。
C# 中的内联
在 C# 中,内联发生在 JIT 级别,JIT 编译器在其中做出决定。 目前 C# 中没有任何机制可以明确执行此操作。 如果您想知道 JIT 编译器正在做什么,那么您可以在运行时调用:
System.Reflection.MethodBase.GetCurrentMethod().Name
。 如果该方法是内联的,它将返回调用者的名称。在 C# 中,您不能强制方法内联,但可以强制方法不内联。 如果您确实需要访问特定的调用堆栈并且需要删除内联,则可以使用:
MethodImplAttribute
和MethodImplOptions.NoInlined
。 此外,如果一个方法被声明为虚拟方法,那么它也不会被 JIT 内联。 其背后的原因是通话的最终目标未知。有关内联的详细信息
What it is
In the terms of C and C++ you use the inline keyword to tell the compiler to call a routine without the overhead of pushing parameters onto the stack. The Function instead has it's machine code inserted into the function where it was called. This can create a significant increase in performance in certain scenarios.
Dangers
The speed benefits in using "inlining" decrease significantly as the size of the inline function increases. Overuse can actually cause a program to run slower. Inlining a very small accessor function will usually decrease code size while inlining a very large function can dramatically increase code size.
Inlining in C#
In C# inlining happens at the JIT level in which the JIT compiler makes the decision. There is currently no mechanism in C# which you can explicitly do this. If you wish to know what the JIT compiler is doing then you can call:
System.Reflection.MethodBase.GetCurrentMethod().Name
at runtime. If the Method is inlined it will return the name of the caller instead.In C# you cannot force a method to inline but you can force a method not to. If you really need access to a specific callstack and you need to remove inlining you can use:
MethodImplAttribute
withMethodImplOptions.NoInlining
. In addition if a method is declared as virtual then it will also not be inlined by the JIT. The reason behind this is that the final target of the call is unknown.More on inlining
有关内联的定义和讨论,请参阅此问题。
在 C# 中,您不需要指示编译器或运行时进行内联,因为运行时会自动执行此操作。
PS 这个答案对于 Java 来说也是正确的。
For definition and discussion of inlining, see this question.
In C# you don't need to instruct the compiler or run-time to inline because the run-time will do it automatically.
P.S. This answer is also correct for Java.
内联是指不调用特定函数,而是直接在调用站点完成函数内容。 这样做的主要原因是,它通过在调用站点运行函数内容来消除调用函数的开销。
它被用作一种优化技术。 就 C# 而言,它实际上依赖于 CLR JIT 引擎来完成工作。 在运行时,编译方法时,JIT 引擎将使用启发式方法来确定内联函数是否合适以及是否会进行有效的优化。 如果是,则执行内联。
Vance 有一篇关于 JIT 在内联时考虑的内容的精彩文章:http://blogs.msdn.com/vancem/archive/2008/08/19/to-inline-or-not-to-inline-that -is-the-question.aspx
在 C# 中无法强制内联。 该语言不支持这种构造。 主要是因为人们对于应该优化什么的判断力很差。 JITer 是一个更好的判断者。
Inlining is when instead of calling a particular function, the contents of the function are done directly at the call site. The main reason for doing this is that it removes the overhead of calling a function by running the function contents at the callsite.
It is used as an optimization technique. In the case of C#, it actually depends on the CLR JIT engine to do the work. At runtime, when compiling a method, the JIT engine will use a heuristic to determine if a function inline is appropriate and will be an effictive optimization. If so, an inline is performed.
Vance has a great article on what the JIT considers when inlining: http://blogs.msdn.com/vancem/archive/2008/08/19/to-inline-or-not-to-inline-that-is-the-question.aspx
There is no way to force an inline in C#. The language does not support this construct. Mainly because people are very poor judges of what should be optimized. The JITer is a much better judge.
引用 Eric Gunnerson
内联发生在 JIT 级别,并且 JIT通常会做出不错的决定
To quote Eric Gunnerson
inlining happens at the JIT level, and the JIT generally makes a decent decision