如何编写Java JIT优化友好的代码?
当您想要从代码中榨取最后一点性能时,您会希望尽可能地利用 JIT 优化。例如,将方法标记为final以方便方法内联,避免关键位置的多态性等。
但是我找不到java程序员可以用来“提示”JIT编译器以获得更快代码的任何参考或选项列表?难道我们不应该有一个来自 JIT 的低延迟性能的“最佳编程”实践列表吗?
When you want to squeeze the last bit of performance from your code, you would want to utilize the JIT optimization as best as you can. For example, marking method final for easy method inlining , avoid polymorphism in critical places, etc.
But I couldn't find any reference or list of options that a java programmer can use to 'hint' the JIT compiler for faster code? Shouldn't we have a list of 'best programming' practice for low latency performance from JIT ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写 JIT 友好代码的最佳方法是编写直接、简单的代码,因为这是 JIT 寻找的内容并且知道如何优化。没有任何技巧!
另外,不同的 JVM 有不同的 JIT,因此为了确保您的代码能够与所有这些 JVM 良好配合,您不能依赖它们中的任何一个。
提高 JIT 性能的常用方法是通过 JVM 的外部配置。由于当今大多数 JVM 都知道如何直接内联
代码小方法调用,因此大多数性能提升来自配置垃圾收集器。这里花费了大量的精力来避免在收集时停止程序,并且您可以根据了解底层硬件的配置方式以及哪些功能比其他硬件更好的方式进行调整。但 Java 代码则不然,它需要直接、简单。The best way to write JIT-friendly code is to write straight, simple code because this is what the JIT looks for and knows how to optimize. No tricks!
Also different JVM's have different JIT's, so in order to be certain your code works well with all of them you must not rely on any of them.
The usual way to improve JIT-performance is through external configuration of the JVM. As most JVM's these days know how to inline
codesmall method calls directly, most performance gains come from configuring the garbage collector. Here much effort is used in avoiding having to stop your program while collecting, and you can tweak quite a bit with your knowledge of how the underlying hardware is configured and what works better than others. But not the Java code, it needs to be straight and simple.HotSpot 作者和其他人一再指出,最好的方法是编写简单直接的代码。
The HotSpot authors and others have repeatedly stated that the best approach is to write simple straightforward code.