Method Inlining in HotSpot

H

hemartin

Hi,
I just did some experiments to gain insides about inlining of method
call in HotSpot. My setup was the following: there exists a Test class
and a Store class. Test calls a getter to retrieve a variable with an
array of Store:

class Store {
int arr[];
...
}

In short, there are 4 ways to retrieve this variable:

class Store {
public int get1(int idx) { return arr[idx] };
public final int get2(int idx) { return arr[idx] };
public static int get3(Store s, int idx) { return s.arr[idx] };
}

The forth method is inlining with in Test:

class Test {
public void test() {
Store store = new Store();
int x = store.arr[idx];
...
}
}

The results of benchmarking these four approaches were leveled to
"inlined":
inlined: 100% time usage
static: 107% time usage
final: 117% time usage
virtual: 117% time usage

Question: if HotSpot would inline the static and final methods
correctly, wouldn't there be no overhead at all for get2() and get3()?

Thanks for your response.
Martin

==
Appended are my two benchmark classes and the result of on run:

public class Store {
int[] arr = new int[] {0,1,2,3,4,5,6,7,8,9};

public int get(int idx) {
return arr[idx];
}
public final int get2(int idx) {
return arr[idx];
}
public static int get3(Store store, int idx) {
return store.arr[idx];
}
}

public class Test {
private final int max = (int) 1e8;

public static void main(String[] args) {
new Test().test();
}
private void test() {
Store store = new Store();
long before, after;
long ret = 0;

for (int i = 0; i < 10; i++) {
// inlined access ------------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += store.arr[j & 7];
}
after = System.currentTimeMillis();
System.out.println("inlined: " + (after - before) + " ms");
}

for (int i = 0; i < 10; i++) {
// virtual call --------------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += store.get(j & 7);
}
after = System.currentTimeMillis();
System.out.println("virtual: " + (after - before) + " ms");
}

for (int i = 0; i < 10; i++) {
// call to final method ------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += store.get2(j & 7);
}
after = System.currentTimeMillis();
System.out.println("final : " + (after - before) + " ms");
}

for (int i = 0; i < 10; i++) {
// static call ---------------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += Store.get3(store, j & 7);
}
after = System.currentTimeMillis();
System.out.println("static : " + (after - before) + " ms");
}

System.out.println("\nRound 2:");

for (int i = 0; i < 10; i++) {
// inlined access ------------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += store.arr[j & 7];
}
after = System.currentTimeMillis();
System.out.println("inlined: " + (after - before) + " ms");
}

for (int i = 0; i < 10; i++) {
// virtual call --------------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += store.get(j & 7);
}
after = System.currentTimeMillis();
System.out.println("virtual: " + (after - before) + " ms");
}

for (int i = 0; i < 10; i++) {
// call to final method ------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += store.get2(j & 7);
}
after = System.currentTimeMillis();
System.out.println("final : " + (after - before) + " ms");
}

for (int i = 0; i < 10; i++) {
// static call ---------------------------------------------
before = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
ret += Store.get3(store, j & 7);
}
after = System.currentTimeMillis();
System.out.println("static : " + (after - before) + " ms");
}
System.out.println(ret);
}
}

Output:
....(skipped since uncertain results for first round)
Round 2:
inlined: 409 ms
inlined: 415 ms
inlined: 408 ms
inlined: 408 ms
inlined: 409 ms
inlined: 409 ms
inlined: 409 ms
inlined: 409 ms
inlined: 409 ms
inlined: 407 ms
virtual: 482 ms
virtual: 500 ms
virtual: 486 ms
virtual: 486 ms
virtual: 486 ms
virtual: 486 ms
virtual: 483 ms
virtual: 487 ms
virtual: 482 ms
virtual: 484 ms
final : 481 ms
final : 488 ms
final : 486 ms
final : 485 ms
final : 484 ms
final : 486 ms
final : 481 ms
final : 481 ms
final : 481 ms
final : 481 ms
static : 439 ms
static : 448 ms
static : 440 ms
static : 443 ms
static : 439 ms
static : 439 ms
static : 439 ms
static : 439 ms
static : 439 ms
static : 438 ms
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top