7

I found out that the C++ compiler does so but I want to know if the Java compiler does the same since in that answer they said adding static would do so but static is different in java and C++. In my case performance would matter since am using functions that are called only once per frame in a game loop and called nowhere else, to make it more readable
In my code I have it setup up similar to this, except with many more calls

while(running)
{
    update();
    sync();
}

and then update(), render() would call more methods that call other methods

private final void update()
{
    switch(gameState)
    {
        case 0:
            updateMainMenu();
            renderMainMenu();
            break;
        case 1:
            updateInGame();
            renderInGame();
            break;
         //and so on
    }
}

private final void updateInGame()
{
    updatePlayerData();
    updateDayCycle();
    //and so on
}

private final void updatePlayerData()
{
    updateLocation();
    updateHealth();
    //and so on
}

So would the compiler inline these functions since they are only used once per frame in the same location?

If this is a bad question, plz tell me and I will remove it.

6

2 Answers 2

5

A Java JITC will attempt to inline any functions that appear (based on runtime statistics) to be called often enough to merit it. It doesn't matter whether the function is called in only one place or dozens of places -- each calling site is analyzed separately.

Note that the decision is based on several factors. How big the method is is one -- if there are a lot of potential inlining candidates only the most profitable will be inlined, to avoid "code bloat". But the frequency of the call (multiplied by the perceived expense of the call) is the biggest "score" factor.

One thing that will discourage inlining is obvious polymorphic calls. If a call might be polymorphic it must be "guarded" by code that will execute the original call if the arriving class is not the expected one. If statistics prove that a call is frequently polymorphic (and including all the polymorphic variants is not worthwhile) then it's likely not sufficiently profitable to inline. A static or final method is the most attractive, since it requires no guard.

Another thing that can discourage inlining (and a lot of other stuff) is, oddly enough, a failure to return from the method. If you have a method that's entered and then loops 10 million times internally without returning, the JITC never gets a chance to "swap out" the interpreted method and "swap in" the compiled one. But JITCs overcome this to a degree by using techniques for compiling only part of a method, leaving the rest interpreted.

3
  • 1
    You think it's odd that a long-running isn't inlined? Why shoud it get inlined? The overhead of the method call will be, relatively speaking, negligible. Nov 17, 2015 at 0:41
  • @EugeneBeresovsky - But the called method never gets optimized, even if it loops a million times inside.
    – Hot Licks
    Nov 17, 2015 at 1:16
  • Oh, I see. Makes sense. Nov 17, 2015 at 1:24
4

For future reference, you can view the bytecode of a .class file with javap -c MyClass to see what your compiled code looks like.

To answer your question: the Java compiler does not inline methods. The JVM, on the other hand, analyzes your code and will inline at runtime if necessary. Basically, you shouldn't worry about it -- leave it to the JVM, and it will inline if it finds it beneficial. The JVM is typically smarter than you when it comes to these things.


From http://www.oracle.com/technetwork/java/whitepaper-135217.html#method:

Method Inlining
The frequency of virtual method invocations in the Java programming language is an important optimization bottleneck. Once the Java HotSpot adaptive optimizer has gathered information during execution about program hot spots, it not only compiles the hot spot into native code, but also performs extensive method inlining on that code.

Inlining has important benefits. It dramatically reduces the dynamic frequency of method invocations, which saves the time needed to perform those method invocations. But even more importantly, inlining produces much larger blocks of code for the optimizer to work on. This creates a situation that significantly increases the effectiveness of traditional compiler optimizations, overcoming a major obstacle to increased Java programming language performance.

Inlining is synergistic with other code optimizations, because it makes them more effective. As the Java HotSpot compiler matures, the ability to operate on large, inlined blocks of code will open the door to a host of even more advanced optimizations in the future.

13
  • Wait but does the JVM only do it when it's run? Couldnt I do it before to make the code smaller?
    – user3213315
    Jul 24, 2014 at 0:08
  • @androidmaster The compiler won't do it for you, and yes the JVM/JIT will do it at runtime. What do you mean by "smaller"? You mean source file size?
    – arshajii
    Jul 24, 2014 at 0:09
  • 1
    To be purist Java Language compiler does not inline methods. Java bytecode compiler does. Jul 24, 2014 at 0:09
  • @Vash-DamianLeszczyński This is typically what we mean by the term "Java compiler".
    – arshajii
    Jul 24, 2014 at 0:10
  • 2
    @arshajii I think the idea of "you shouldn't worry about things like this" is counterproductive when targeting developers. It's better to point the reader to JVM whitepapers, JDK source code references describing the algorithms used etc. We are developers, we want to know how it works under the hood, and especially: we want to learn about things that can prevent things from being able to be inlined. Dec 29, 2020 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.