![]() |
Method Inlining in HotSpot
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 |
| All times are GMT. The time now is 07:55 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.