Method Inlining in HotSpot

Discussion in 'Java' started by hemartin, Mar 17, 2008.

  1. hemartin

    hemartin Guest

    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
    hemartin, Mar 17, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WJ

    Graphic HotSpot ?

    WJ, Apr 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    411
  2. VB Programmer

    How create image hotspot?

    VB Programmer, Sep 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    10,579
    David Wier
    Sep 27, 2004
  3. gh

    US MAP\Hotspot Component

    gh, Jul 28, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    349
  4. Naresh Agarwal
    Replies:
    9
    Views:
    7,573
    Brad BARCLAY
    Jan 8, 2004
  5. Replies:
    15
    Views:
    592
    Tobias Müller
    Nov 10, 2012
Loading...

Share This Page