Java `Inline` Function Question

statik213

Golden Member
Oct 31, 2004
1,654
0
0
I was looking around to see if you can inline functions in java and I came across this:

# No inline methods. The Java compiler might decide on its own to inline a method, but you don?t have much control over this. You can suggest inlining in Java by using the final keyword for a method. However, inline functions are only suggestions to the C++ compiler as well.

from: http://www.javacoffeebreak.com/articles/thinkinginjava/comparingc++andjava.html

Now, does that mean that only final methods can be inlined? Or will the compiler inline non-final methods also?
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
javac is more likely to inline a final method because it doesn't have to worry about the method being overloaded in inherited classes.
 

znaps

Senior member
Jan 15, 2004
414
0
0
I think it inlines other things also, like private methods. The runtime also does inlining.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
That article is pretty old so I imagine runtime system technology has progressed a lot since then. Like znaps said, I imagine a good JIT compiler can inline lots of stuff at runtime, final or not. Not that I know a whole lot about VMs.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
If anyone knows of any good articles about this sort of stuff please point me towards it. I'd like to know more about how the compiler/VM inlines stuff.
For example the function Math.max(a,b) should defintely be inlined, and I have a bunch of functions like these in my own code that I probably should make final to help java/c inline.

Thanks for the feedback guys....

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
i never understood the allure of putting an inline directive in the code. compilers are pretty good at this kind of optimization these days.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: oog
i never understood the allure of putting an inline directive in the code. compilers are pretty good at this kind of optimization these days.


Well, since most people (including myself) dont know exactly what criteria compilers uses to check for a `inlineability`, it's nice to have the option to be able to define (or suggest) a function as inline explicitly....
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
I agree that most people (other than the compiler writers or people who look at the compiler code) don't know the criteria for checking if something should be inlined. It's not governed by any standard. The thing I question is whether or not the vast majority of developers can do a better job of picking what to inline than the compiler.

There is a tradeoff to inlining code. You'll end up with a larger executable and therefore larger working set when the code gets loaded (not necessarily a bad thing in itself, but it's bad in some circumstances or if taken to extremes). And in C++ it means that the compiler needs to know the definition (as opposed to the declaration) of a function at compile time, instead of just at the time of linking. Either you inline only private or non-virtual protected functions, or you expose the definition of the function in the header file, which is generally a bad thing unless you're talking about templates.

I know that the OP was talking about Java, but inlining mostly comes up in C++ since Java has no direct support for the developer saying what to inline.