什么是方法内联? [英] What is method inlining?

查看:302
本文介绍了什么是方法内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图理解它的真正含义:


内联函数



在C ++中,
中定义的成员函数是类声明。 (2)函数
调用编译器用
替换函数的实际代码。
关键字内联可用于暗示
编译器执行内联
扩展的成员体或
非成员函数。



内联


期间用函数代码的副本
替换函数调用

举例来说,它是这样写的:


当一个方法是final时,它可能是
内置的。


这里: http://www.roseindia.net/javatutorials/final_methods.shtml



你能给我一个例子或者什么,或者基本上帮助我理解它可以被内联的意思。



谢谢。

解决方案

Inlining是由Java Just-In-Time编译器执行的优化。 >如果你有一个方法d:

  public int addPlusOne(int a,int b){
return a + b + 1;
}

你可以这样称呼:

  public void testAddPlusOne(){
int v1 = addPlusOne(2,5);
int v2 = addPlusOne(7,13);

//用v1,v2
做一些事情}

编译器可能会决定将函数调用替换为函数体,所以结果将如下所示:

  public void testAddPlusOne(){
int v1 = 2 + 5 + 1;
int v2 = 7 + 13 + 1

//用v1,v2
做一些事情

编译器会这样做,以节省实际进行函数调用的开销,这会涉及将每个参数推送到堆栈。



这显然只能用于非虚拟功能。考虑如果方法在子类中被覆盖并且包含该方法的对象的类型在运行时才知道,会发生什么......编译器如何知道要复制的代码:基类的方法体或子类方法体?由于所有的方法在Java中默认都是虚拟的,所以你可以明确地标记那些不能被重写为 final (或者把它们放到 final class)。这将帮助编译器确定该方法永远不会被覆盖,并且内联是安全的。 (请注意,编译器有时也会对非final方法做出此决定。)



另外,请注意引用中的 may 一词。最终的方法不保证是可以接受的。有多种方法可以保证方法不能被内联,但没有办法强制编译器内联。当内联可以帮助我们的时候,它会比你更清楚地知道它会对你造成的代码速度产生什么影响。

维基百科,了解收益和问题。


I've been trying to understand what that really means :

inline function

In C++, a member function defined in the class declaration. (2) A function call that the compiler replaces with the actual code for the function. The keyword inline can be used to hint to the compiler to perform inline expansion of the body of a member or nonmember function.

inline

To replace a function call with a copy of the function's code during compilation.

For example it is written something like :

When a method is final, it may be inlined.

Here : http://www.roseindia.net/javatutorials/final_methods.shtml

Can you give me an example or something or basically help me to understand what "it may be inlined" means.

Thanks.

解决方案

Inlining is an optimization performed by the Java Just-In-Time compiler.

If you have a method:

public int addPlusOne(int a, int b) {
  return a + b + 1;
}

which you call like this:

public void testAddPlusOne() {
  int v1 = addPlusOne(2, 5);
  int v2 = addPlusOne(7, 13);

  // do something with v1, v2
}

the compiler may decide to replace your function call with the body of the function, so the result would effectively look like this:

public void testAddPlusOne() {
  int v1 = 2 + 5 + 1;
  int v2 = 7 + 13 + 1

  // do something with v1, v2
}

The compiler does this to save the overhead of actually making a function call, which would involve pushing each parameter on to the stack.

This can clearly only be done for non-virtual functions. Consider what would happen if the method was overriden in a sub class and the type of the object containing the method isn't known until runtime...how would the compiler know what code to copy: the base class's method body or the sub class's method body? Since all methods are virtual by default in Java, you can explicitly mark those which cannot be overriden as final (or put them into a final class). This will help the compiler figure out that method will never be overriden, and it is safe to inline. (Note that the compiler can sometimes make this determination for non-final methods as well.)

Also, note the word may in the quote. Final methods aren't guaranteed to be inlineable. There are various ways you can guarantee a method isn't capable of being inlined, but no way to force the compiler to inline. It will almost always know better than you anyway when inlining will help vs. hurt the speed of the resulting code.

See wikipedia for a good overview of benefits and problems.

这篇关于什么是方法内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆