JVM method inlining

1. The calling process of the function.

Calling a function actually transfers the execution sequence of the program to a certain address in the memory where the function is stored. After the program content of the function is executed, it returns to the place before the execution of the function.
This kind of transfer operation requires that the site be protected and the executed address must be remembered before the transfer, and the site must be restored after the transfer, and continue to execute according to the original saved address. This is usually called pushing and popping .
Therefore, function calls must have a certain time and space overhead. So for those functions whose body code is not very large and frequently called, this time and space consumption will be very large.

2. Inline function
So how to solve this performance consumption problem, this time you need to introduce inline function. Inline function means that when the program is compiled, the compiler directly replaces the call expression of the inline function appearing in the program with the function body of the inline function . Obviously, this will not cause the problem of reverting to reverting, but because the code in the function body is substituted into the program at compile time, it will increase the amount of target program code, which in turn increases the space overhead, and there is no time consignment sale. It is as big as the function call, which shows that it is a time saving at the expense of the increase of the target code.

写C代码时,我们都学到将一些简短的逻辑定义在宏里。这样做的好处是,在编译器编译的时候会将用到该宏的地方直接用宏的代码替换。这样就不再需要象调用方法那样的压栈、出栈,传参了。性能上提升了。内联函数的处理方式与宏类似,但与宏又有所不同,内联函数拥有函数的本身特性(类型、作用域等等)。在C++里有个内联函数,使用inline关键字修饰。另外,写在Class定义内的函数也会被编译器视为内联函数。 

3. JVM inline functions

Whether C++ is an inline function is determined by itself, and Java is determined by the compiler. Java does not support direct declaration as inline functions. If you want them to be inlined, you can only make a request to the compiler: The keyword final modification is used to indicate which function is to be inlined by the JVM, for example:

public final void doSomething() {  
        // to do something  
}  

In general, general functions will not be treated as inline functions. Only after final declarations, the compiler will consider whether to turn your functions into inline functions.

There are many runtime optimizations built into the JVM. First, the short method is more conducive to JVM inference . The process is more obvious, the scope is shorter, and the side effects are more obvious. If it is a long method, the JVM may just kneel down . The second reason is even more important: in the United methods
if some small way to monitor the JVM is frequently executed, it will call the method of replacing the method itself . For example, the following:

private int add4(int x1, int x2, int x3, int x4) {  
        return add2(x1, x2) + add2(x3, x4);  
    }  

    private int add2(int x1, int x2) {  
        return x1 + x2;  
    }  

After running for a period of time, the JVM will remove the add2 method and translate your code into:

private int add4(int x1, int x2, int x3, int x4) {  
        return x1 + x2 + x3 + x4;  
    }  

Guess you like

Origin blog.csdn.net/ke_weiquan/article/details/51946174