Would Java inline method(s) during optimization?

后端 未结 6 2048
青春惊慌失措
青春惊慌失措 2020-11-29 05:46

I wonder if JVM/javac is smart enough to turn

// This line...
string a = foo();

string foo()
{
  return bar();
}

string bar()
{
  return some-complicated-s         


        
6条回答
  •  生来不讨喜
    2020-11-29 06:18

    A "highly optimizing" JIT compiler will inline both cases (and, @Mysticial, it might even inline some polymorphic cases, by employing various forms of trickery).

    You can increase the chances of inlining by making methods final, and a few other tricks.

    javac does some primitive inlining, mostly of final/private methods, primarily intended to help out some conditional compilation paradigms.

提交回复
热议问题