Java 中的边界检查

发布于 2024-10-08 10:11:18 字数 86 浏览 9 评论 0原文

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

"Hotspot can remove bounds checking in Java." Can any one explain this please? Actually im analysing the differences between C++ and Java. It is not a homework and im analysing on my own interest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新人笑 2024-10-15 10:11:18

在谷歌搜索“热点边界检查”后,一篇标题为“Java HotSpot 的数组边界检查消除”的论文™ Client Compiler” 显示(作为第一个结果)并为我们提供了一些见解:

摘要:

每当访问数组元素时,
Java虚拟机执行一个
比较指令以确保
索引值在有效范围内
界限。这减少了执行
Java 程序的速度。数组边界
检查消除标识
此类检查的情况
多余的,可以删除。我们
呈现数组边界检查
Java 的消除算法
基于静态分析的HotSpot™虚拟机
在即时编译器中。

该算法适用于中间体
静态单一表示
分配表并维护
索引表达式的条件。它
如果可以的话,完全删除边界检查
被证明他们永远不会失败。
只要有可能,它就会突破界限
检查是否跳出循环。静态号码
检查数量保持不变,但
检查循环内部可能是
更频繁地执行。如果这样的检查
失败,执行程序失败
返回解释模式,避免
抛出异常的问题
错误的地方。

评估显示加速接近
的理论最大值
科学的 SciMark 基准测试套件
(平均 40%)。该算法还
提高了执行速度
SPECjvm98 基准测试套件(2%
平均,最大 12%)。

马克·梅奥很好地解释了这一点。

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

After googling "hotspot bounds checking", a Paper with the Title "Array Bounds Check Elimination for the Java HotSpot™ Client Compiler" shows up (as the first result) and gives us some insight:

Abstract:

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 explained this nicely.

Bottom line: if Hotspot detects that it isn't necessary to check bounds for an array, it sees this as an oportunity to disable bounds checking for that array and therefore increase performance.

深爱成瘾 2024-10-15 10:11:18

它的工作原理是不断分析程序的性能,寻找可能频繁或重复执行的“热点”,然后针对性能关键性较低的代码进行优化,以最小的开销实现高性能执行。

因此,理论上,如果存在一些边界检查,并且通过重复和频繁的执行可以明显看出它不可能超出边界,那么热点可能会优化这些检查。并不意味着它是绝对正确的,但这可能是它发生的原因之一。

来自 Würthinger 等人的 2007 年文章:“每当访问数组元素时, Java 虚拟机执行比较指令以确保索引值在有效范围内,这会降低 Java 程序的执行速度。数组边界检查消除识别出此类检查是多余的并且可以删除的情况。基于即时编译器中的静态分析的 Java HotSpot™ VM 的检查消除算法。”

Well it works by continually analyzing the program's performance looking for 'hotspots' that might be frequently or repeatedly executed, which are then targeted for optimization for high performance execution with minimum overhead, for less performance-critical code.

So in theory if there is some bounds checking and it's evident through repeated and frequent execution that it's impossible for it to exceed the bounds, hotspots might optimize out those checks. Doesn't mean it's infallible, but that may be one reason why it happens.

From a 2007 article by Würthinger et al: "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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文