了解避免死代码消除后果
Understanding avoiding Dead-code eliminations consequnces
我正在阅读 JMH 示例,现在我在有关 safe-looping 的部分。这是一个例子:
@Benchmark
public void measureRight_2() {
for (int x : xs) {
sink(work(x));
}
}
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public static void sink(int v) {
// IT IS VERY IMPORTANT TO MATCH THE SIGNATURE TO AVOID AUTOBOXING.
// The method intentionally does nothing.
}
但是他们对这项技术提出了一个警告(强调我的)。
Sometimes, the cost of sinking the value into a Blackhole is
dominating the nano-benchmark score. In these cases, one may try to do
a make-shift "sinker" with non-inlineable method. This trick is very
VM-specific, and can only be used if you are verifying the generated
code (that's a good strategy when dealing with nano-benchmarks
anyway).
我不太明白这里具体的 VM 是什么。我们使用了一种不应该内联的方法。因此无法优化计算。我错过了什么细节?
不能保证:
- 无内联提示已正确传达给 JVM;
- JVM 没有故意忽略非内联提示;
- JVM 不会意外忽略非内联提示;
- 非内联提示有效,但 JVM 采用跨方法优化,避免为已知的空方法准备参数;
- 非内联提示有效,但 JVM 还采用了一些其他神奇的优化,您在 15 分钟的思考中无法想出;
这可能会随着 JVM 系列的不同而发生变化,甚至在给定 JVM 系列的 minor/patch 版本中也会发生变化。这就是为什么据说它是非常特定于 VM 的,并且只有在您实际控制所发生的事情时才使用它。
我正在阅读 JMH 示例,现在我在有关 safe-looping 的部分。这是一个例子:
@Benchmark
public void measureRight_2() {
for (int x : xs) {
sink(work(x));
}
}
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public static void sink(int v) {
// IT IS VERY IMPORTANT TO MATCH THE SIGNATURE TO AVOID AUTOBOXING.
// The method intentionally does nothing.
}
但是他们对这项技术提出了一个警告(强调我的)。
Sometimes, the cost of sinking the value into a Blackhole is dominating the nano-benchmark score. In these cases, one may try to do a make-shift "sinker" with non-inlineable method. This trick is very VM-specific, and can only be used if you are verifying the generated code (that's a good strategy when dealing with nano-benchmarks anyway).
我不太明白这里具体的 VM 是什么。我们使用了一种不应该内联的方法。因此无法优化计算。我错过了什么细节?
不能保证:
- 无内联提示已正确传达给 JVM;
- JVM 没有故意忽略非内联提示;
- JVM 不会意外忽略非内联提示;
- 非内联提示有效,但 JVM 采用跨方法优化,避免为已知的空方法准备参数;
- 非内联提示有效,但 JVM 还采用了一些其他神奇的优化,您在 15 分钟的思考中无法想出;
这可能会随着 JVM 系列的不同而发生变化,甚至在给定 JVM 系列的 minor/patch 版本中也会发生变化。这就是为什么据说它是非常特定于 VM 的,并且只有在您实际控制所发生的事情时才使用它。