Let's experiment with Java inlining

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.

What is inline expansion in the first place?

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.

Experiment

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.

result

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.

reference

https://docs.oracle.com/javase/jp/8/docs/technotes/tools/unix/java.html

Recommended Posts

Let's experiment with Java inlining
Let's scrape with Java! !!
Let's operate Excel with Java! !!
Let's study Java
Let's try WebSocket with Java and javascript!
Let's write Java file input / output with NIO
[LeJOS] Let's control the EV3 motor with Java
Let's create a timed process with Java Timer! !!
Install java with Homebrew
Let's touch on Java
Change seats with java
Install Java with Ansible
Comfortable download with JAVA
Switch java with direnv
Download Java with Ansible
Build Java with Wercker
Endian conversion with JAVA
[Java basics] Let's make a triangle with a for statement
[LeJOS] Let's remotely control the EV3 motor with Java
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Run batch with docker-compose with Java batch
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Java to play with Function
Try DB connection with Java
Amazing Java programming (let's stop)
Enable Java EE with NetBeans 9
[Java] JavaConfig with Static InnerClass
Try gRPC with Java, Maven
[Form_with] Let's unify form with form_with.
Version control Java with SDKMAN
RSA encryption / decryption with java 8
Paging PDF with Java + PDFBox.jar
Sort strings functionally with java
Object-oriented (java) with Strike Gundam
[Java] Let's replace data objects with a mapper ~ BeanMapper Orika ~
[Java] Content acquisition with HttpCliient
Java version control with jenv
Troubleshooting with Java Flight Recorder
Streamline Java testing with Spock
Connect to DB with Java
Connect to MySQL 8 with Java
Error when playing with java
Using Mapper with Java (Spring)
Java study memo 2 with Progate
Getting Started with Java Basics
Seasonal display with Java switch
Use SpatiaLite with Java / JDBC
Study Java with Progate Note 1
Compare Java 8 Optional with Swift
HTML parsing with JAVA (scraping)
Run Java VM with WebAssembly
Screen transition with swing, java
Java unit tests with Mockito