performance of many method calls

hello, since i am too lazy to do measures myself, i am asking you guys here if you know details about the performance loss of many method calls.

example


public void functionWithLotsOfCode() {
 // lots of code here
}

the code in this function could be split up to three functions and then called like this:


public void functionWithLotsOfCode() {
 functionA();
 functionB();
 functionC();
}

will the second option be significantly slower? imagine the function really does a lot of work and is called very often!

Shouldn’t make any difference. Test it.

Cas :slight_smile:

This isn’t really true – it DOES make a difference – but the advice is about as good as it gets… Test it!
Write the code so it works first, and write it as readable and expandable as possible. After it works, if you’ve got performance problems, run a profiler and find the bottlenecks. If it turns out the program spends 20% of the cpu time calling methods from some inner loop, you might want to inline it.

ok thanks guys!

well yeah, testing… but i’m so lazy. :wink:

Yes, it can definitely make a (sometimes even huge) difference to manually inline, but will it be worth it? (i.e. does your profiler say this code is your bottleneck?).
OTOH I have also seen cases where manual inlining caused massive slow downs.

In any case, to fix performance problems, you will have to do testing eventually. :stuck_out_tongue:
And more importantly, don’t even consider such ugly hacks like this before you even have a performance problem!

Does someone have facts under what conditions the c1 or c2 hotspot compilers are able to inline methods?

That’s an excellent question.

I usually just find out by profiling and trial & error, but I usually end up only inlining something in tight inner loops where my program spends most time in (and then I always come to the conclusion that there’s still a lot potential left in HotSpot).
But I’d also like to know more about the facts of when HotSpot is able to inline.

Take a look at the recently revised HotSpot white paper and at the various publications on the HotSpot publications page. There are some papers and presentations on the latter that describe the algorithms used. Both compilers use class hierarchy analysis to enable deep inlining but the C2 compiler also uses type profiling and, more recently, bimorphic call inlining where profitable.