Inlining of method calls in JVM

Was listening to one of the Oracle TechCasts today: Towards a Universal VM. The talk was surrounded on the different capabilities of JVM. And there was a mention about inlining methods in JVM. So I’ve decided to write a small test to see how does inlining influence the performance. Test simply concatenates two strings.

So I’ve looked at three variants here:

  1. Direct concatenation. Something like

    val1 + val2

    which’s replaced with StringBuilder#append underneath by the compiler.
  2. Static method invocation. Something like:

    obj.doSomething(val1, val2)
  3. Reflection method invocation. Something like:

    method.invoke(obj, val1, val2)

Inlining of the methods is enabled in Sun JVM by default and can be disabled by passing -XX:-Inline parameter to the JVM. So below you can find the results of invoking concatenation of two string for 1000000 times:

Inline EnabledInline Disabled
Attempt 1142713601465192920142437
Attempt 2147115471581195920932477
Attempt 3144014851425199320672381
Attempt 4161914951507203021142515
Attempt 5139914951530202120842401

As you can see – the results are the following:

  1. Using internal inlining of the methods definitely makes JVM faster.
  2. Overhead of invoking methods is really small in comparison to the using some code directly
  3. Invoking method through a reflection is really small in case of inlining, less than 2%. However it’s about 18% in case of the disabled inlining.

In general  it’s possible to say that modern JVMs enable inlining by default and it really makes your Java code running faster. Also it seems like inlining of the methods makes reflection method invocation work really fast and you don’t need to afraid of the reflection any more..