3

My assumption is that, with compiler optimization (e.g., inlining), it makes practically no difference whether a method is "nested" a few levels. Would that be the case indeed?

For example, say that the following 3 classes are declared:

public class Third extends Second
{
    public int test3() // Call test2() and nothing else
    {
        return super.test2();
    }
}

public class Second extends First
{
    public int test2()
    {
        return super.test1(); // Call test1() and nothing else
    }
}

public class First
{
    public int test1() // Calculate a result somehow
    {
        int result = 0;
        ...
        return result;
    }
}

Given 3 already instantiated objects third, second and first, is the cost of the following calls practically the same?

third.test3();
third.test2();
third.test1();
second.test2();
second.test1();
first.test1();

Would it make any difference in optimization if the name of the method was the same?

PNS
  • 19,295
  • 32
  • 96
  • 143
  • Why don't you measure it your self and see? – OscarRyz Jan 24 '12 at 00:58
  • There is no reliable way of measuring this in a simple program and thus I am looking for an answer with respect to Java compiler optimization (Hotspot etc.). – PNS Jan 24 '12 at 00:59

2 Answers2

4

Should you worry about method call overhead? Most of the time, no. It's only when you have a method that's called very heavily in a loop that this overhead matters.

Usually, the nanoseconds of time difference mean squat when the work your methods are doing will be the complete bulk of time spent in execution. Further, as you mentioned, the Java HotSpot VM does do inlining of method calls where appropriate. That decision is up the VM however. See this link:

http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html

There are better things to worry about in software development these days. Ship that app!

Mike
  • 8,853
  • 3
  • 35
  • 44
  • 1
    +1 Bravo for pointing out that this would only ever matter in a loop. – user949300 Jan 24 '12 at 01:26
  • The fact is often you don't know how your method will be used (think of something like a logging API). That's why I came to this post and I'm happy to know HotSpot optimised this kind of situation. – zakmck Aug 03 '12 at 15:56
3

Each nested call is going to take up another position on the call stack. Since you're not doing much else in the methods, that will make up the bulk of the 'cost' of calling your methods so it will make a difference.

A completely unoticable difference to the user, but a difference nevertheless.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Sure, but maybe the compiler optimizes (e.g., via inlining), so that this is not necessary? – PNS Jan 24 '12 at 01:00
  • Yeah - if HotSpot inlines even non-final calls, surely it can inline super constructor calls, since constructors are invoked statically and no overriding is possible? Of course, it would initially be 3 separate frames, if you care about early performance... – Daniel Lubarov Jan 24 '12 at 01:04