我可以强制JVM本地编译给定的方法吗?

时间:2022-04-16 14:34:14

I have a performance-critical method called often when my app starts up. Eventually, it gets JIT-compiled, but not after some noticeable time being run in the interpreter.

当我的应用程序启动时,我经常调用一个性能关键的方法。最终,它被JIT编译,但是在解释器中运行了一些明显的时间之后。

Is there any way I can tell the JVM that I want this method compiled right from the start (without tweaking other internals with stuff like -XX:CompileThreshold)?

有没有什么办法可以告诉JVM我想从一开始就编译这个方法(没有用-XX:CompileThreshold之类的东西调整其他内部函数)?

2 个解决方案

#1


33  

The only way I know of is the -Xcomp flag, but that is not generally advisable to use. It forces immediate JIT compilation of ALL classes and methods first time they are run. The downside is that you will see a performance decrease on initial startup (due to increased JIT activity). The other major limitation with this flag is that it appears to disable the incremental profiling-based optimization that JIT would normally do. In standard mixed mode, the JIT compiler can (and will) deoptimize and re-compile parts of the code continually based on profiling and run-time information collected. This allows it to "correct" faulty optimizations like boundary checks that were omitted but turned out to be needed, sub-optimal inlinings etc. -Xcomp disables the profiling-based optimization and depending on program, can cause significant performance losses overall for only a small or no real gain in startup, which is why it's not recommended to use.

我知道的唯一方法是-Xcomp标志,但通常不建议使用。它会在第一次运行时强制立即对所有类和方法进行JIT编译。缺点是您将在初始启动时看到性能下降(由于JIT活动增加)。此标志的另一个主要限制是它似乎禁用JIT通常会执行的基于增量分析的优化。在标准混合模式下,JIT编译器可以(并将)根据收集的分析和运行时信息不断地优化和重新编译部分代码。这允许它“纠正”错误的优化,例如省略但被证明是必需的边界检查,次优内联等。-Xcomp禁用基于分析的优化,并且根据程序,可能导致整体上仅显着的性能损失。启动时很小或没有实际收益,这就是为什么不建议使用它。

Beyond to -Xcomp (which is pretty brutal) and -XX:CompileThreshold (which controls how many executions of a given method the JIT will run in intepreted mode to gather stats before compiling/optimizing it), there is also -Xbatch. This forces JIT compilation to the "foreground", essentially blocking calls to methods until it's been compiled, rather than compiling it in the background as it normally does.

除了-Xcomp(这是非常残酷的)和-XX:CompileThreshold(它控制JIT将在编译/优化它之前在解释模式下运行以获取统计数据的给定方法的执行次数),还有-Xbatch。这会强制JIT编译到“前台”,基本上阻止对方法的调用,直到它被编译,而不是像往常一样在后台编译它。

You didn't specify which Java version you are using, but if Java 7 is an option for you, it introduces a new JIT model called "Tiered compilation" (activated with the -XX:+TieredCompilation switch). What tiered compilation does is that it allows an initial, smaller compilation pass on the first use of a method and than an additional, larger compilation/optimization later, based on collected profiling data. Sounds like it should be interesting to you.

您没有指定您正在使用的Java版本,但如果Java 7是您的选项,它会引入一个名为“Tiered compilation”的新JIT模型(使用-XX:+ TieredCompilation开关激活)。分层编译的作用是,它允许在第一次使用方法时进行初始的,较小的编译传递,而不是基于收集的分析数据进行额外的,更大的编译/优化。听起来应该对你有意思。

It supposedly requires some additional tweaking and parameters/configurations, but I've not got around to checking it out further.

据说它需要一些额外的调整和参数/配置,但我还没有进一步检查它。

#2


2  

im not sure if it'll completely precompile the code, but you could add your class with the critical method to the JVM's shared data dump. see this question for more details.

我不确定它是否会完全预编译代码,但是您可以使用关键方法将您的类添加到JVM的共享数据转储中。有关详细信息,请参阅此问题。

also, have you considered JNI? if your method is very CPU intensive it might speed things up considerably.

还有,你考虑过JNI吗?如果你的方法非常占用CPU,那么它可能会大大加快速度。

#1


33  

The only way I know of is the -Xcomp flag, but that is not generally advisable to use. It forces immediate JIT compilation of ALL classes and methods first time they are run. The downside is that you will see a performance decrease on initial startup (due to increased JIT activity). The other major limitation with this flag is that it appears to disable the incremental profiling-based optimization that JIT would normally do. In standard mixed mode, the JIT compiler can (and will) deoptimize and re-compile parts of the code continually based on profiling and run-time information collected. This allows it to "correct" faulty optimizations like boundary checks that were omitted but turned out to be needed, sub-optimal inlinings etc. -Xcomp disables the profiling-based optimization and depending on program, can cause significant performance losses overall for only a small or no real gain in startup, which is why it's not recommended to use.

我知道的唯一方法是-Xcomp标志,但通常不建议使用。它会在第一次运行时强制立即对所有类和方法进行JIT编译。缺点是您将在初始启动时看到性能下降(由于JIT活动增加)。此标志的另一个主要限制是它似乎禁用JIT通常会执行的基于增量分析的优化。在标准混合模式下,JIT编译器可以(并将)根据收集的分析和运行时信息不断地优化和重新编译部分代码。这允许它“纠正”错误的优化,例如省略但被证明是必需的边界检查,次优内联等。-Xcomp禁用基于分析的优化,并且根据程序,可能导致整体上仅显着的性能损失。启动时很小或没有实际收益,这就是为什么不建议使用它。

Beyond to -Xcomp (which is pretty brutal) and -XX:CompileThreshold (which controls how many executions of a given method the JIT will run in intepreted mode to gather stats before compiling/optimizing it), there is also -Xbatch. This forces JIT compilation to the "foreground", essentially blocking calls to methods until it's been compiled, rather than compiling it in the background as it normally does.

除了-Xcomp(这是非常残酷的)和-XX:CompileThreshold(它控制JIT将在编译/优化它之前在解释模式下运行以获取统计数据的给定方法的执行次数),还有-Xbatch。这会强制JIT编译到“前台”,基本上阻止对方法的调用,直到它被编译,而不是像往常一样在后台编译它。

You didn't specify which Java version you are using, but if Java 7 is an option for you, it introduces a new JIT model called "Tiered compilation" (activated with the -XX:+TieredCompilation switch). What tiered compilation does is that it allows an initial, smaller compilation pass on the first use of a method and than an additional, larger compilation/optimization later, based on collected profiling data. Sounds like it should be interesting to you.

您没有指定您正在使用的Java版本,但如果Java 7是您的选项,它会引入一个名为“Tiered compilation”的新JIT模型(使用-XX:+ TieredCompilation开关激活)。分层编译的作用是,它允许在第一次使用方法时进行初始的,较小的编译传递,而不是基于收集的分析数据进行额外的,更大的编译/优化。听起来应该对你有意思。

It supposedly requires some additional tweaking and parameters/configurations, but I've not got around to checking it out further.

据说它需要一些额外的调整和参数/配置,但我还没有进一步检查它。

#2


2  

im not sure if it'll completely precompile the code, but you could add your class with the critical method to the JVM's shared data dump. see this question for more details.

我不确定它是否会完全预编译代码,但是您可以使用关键方法将您的类添加到JVM的共享数据转储中。有关详细信息,请参阅此问题。

also, have you considered JNI? if your method is very CPU intensive it might speed things up considerably.

还有,你考虑过JNI吗?如果你的方法非常占用CPU,那么它可能会大大加快速度。