Is it true that having lots of small methods helps the JIT compiler optimize?

后端 未结 4 417
夕颜
夕颜 2021-01-30 16:56

In a recent discussion about how to optimize some code, I was told that breaking code up into lots of small methods can significantly increase performance, because the JIT compi

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 17:18

    If you take the exact same code and just break them up into lots of small methods, that is not going to help JIT at all.

    A better way to put it is that modern HotSpot JVMs do not penalize you for writing a lot of small methods. They do get aggressively inlined, so at runtime you do not really pay the cost of function calls. This is true even for invokevirtual calls, such as the one that calls an interface method.

    I did a blog post several years ago that describes how you can see JVM is inlining methods. The technique is still applicable to modern JVMs. I also found it useful to look at the discussions related to invokedynamic, where how the modern HotSpot JVMs compiles Java byte code gets discussed extensively.

提交回复
热议问题