java - Java 中的边界检查

标签 java runtime jvm-hotspot bounds-check-elimination

“Hotspot 可以删除 Java 中的边界检查。”任何人都可以解释一下吗?实际上,我正在分析 C++ 和 Java 之间的差异。这不是家庭作业,我是根据自己的兴趣分析的。

最佳答案

谷歌搜索“热点边界检查”后,a Paper with the Title "Array Bounds Check Elimination for the Java HotSpot™ Client Compiler"出现(作为第一个结果)并给了我们一些见解:

摘要:

Whenever an array element is accessed, Java virtual machines execute a compare instruction to ensure that the index value is within the valid bounds. This reduces the execution speed of Java programs. Array bounds check elimination identifies situations in which such checks are redundant and can be removed. We present an array bounds check elimination algorithm for the Java HotSpot™ VM based on static analysis in the just-in-time compiler.

The algorithm works on an intermediate representation in static single assignment form and maintains conditions for index expressions. It fully removes bounds checks if it can be proven that they never fail. Whenever possible, it moves bounds checks out of loops. The static number of checks remains the same, but a check inside a loop is likely to be executed more often. If such a check fails, the executing program falls back to interpreted mode, avoiding the problem that an exception is thrown at the wrong place.

The evaluation shows a speedup near to the theoretical maximum for the scientific SciMark benchmark suite (40% on average). The algorithm also improves the execution speed for the SPECjvm98 benchmark suite (2% on average, 12% maximum).

Mark Mayo 很好地解释了这一点。

底线:如果 Hotspot 检测到不需要检查数组的边界,它会将此视为禁用该数组边界检查的机会,从而提高性能。

关于java - Java 中的边界检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4469483/

相关文章:

java - 如何将普通位图转换为单色位图android

java - 如何在 tomcat 7 中为 spring boot 1.2.3 制作可部署的 war 文件

java - 为什么序列化 Integer 需要这么多(81)个字节?

go - runtime.Callers 根据运行的位置打印不同的程序计数器

java - 为什么 jmap -permstat 会报告超过使用的 MaxPermSize 字节数?

java - 将多个参数传递给 Pig Filter UDF

java - 为什么我的 HashMap 实现比 JDK 慢 10 倍?

c - pthread 程序中例程的额外执行时间花费了什么?

java - JavaLaunchHelper 类在... libinstrument.dylib 中都实现了。将使用两者之一。哪个是未定义的

java - 调试java混淆代码