I've heard that JIT compilers do inlining in Java, but I wanted to experiment a bit to see how much they benefit from it.
For more information [Wikipedia](https://en.wikipedia.org/wiki/%E3%82%A4%E3%83%B3%E3%83%A9%E3%82%A4%E3%83%B3%E5 See% B1% 95% E9% 96% 8B). The point is that there is overhead in calling a method, so if you embed the processing inside the method on the caller side of the method, you can reduce the cost of calling the method. The JIT compiler does this as needed.
The code to try this time is as follows. I prepared a method that just appends a string in StringBuilder and called it repeatedly. Since the same method is called many times, I hypothesized that the performance might differ depending on whether it is inlined or not.
InlineSample.java
public class InlineSample {
public static void main(String[] args) {
long start = System.currentTimeMillis();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 10_000_000; i++) {
appendHoge(builder);
}
System.out.println(builder.length());
long end = System.currentTimeMillis();
System.out.println("elapsed " + (end - start) + " ms");
}
private static void appendHoge(StringBuilder builder) {
builder.append("hoge");
}
}
We ran this code with both enabled and disabled patterns for inlining to verify performance. Inlining is enabled by default, so I disabled it by adding the following VM parameter:
-XX:-Inline
In addition, I am trying it in the environment of Windows10 and Java15-ea.
After trying 5 times and averaging, the following results were obtained.
Inline expansion ON: Average 125ms
Inline expansion OFF: Average 342ms
It's quite a difference. However, since JIT compilation is performed during program execution, it was not inlined from the beginning of program execution (I checked the inline expansion log with the -XX: + PrintInlining option). It's not a very fair comparison in this regard. I think it would have been better to use the benchmark tool JMH, but anyway, I'm surprised that there is such a difference.
The size of the method that is the target of inline expansion is
-XX:MaxInlineSize=size
Can be specified by. The default is 35 bytes. You may be able to benefit more from the JIT compiler by writing code that is conscious of inline expansion.
https://docs.oracle.com/javase/jp/8/docs/technotes/tools/unix/java.html
Recommended Posts