Java 中的边界检查
“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在谷歌搜索“热点边界检查”后,一篇标题为“Java HotSpot 的数组边界检查消除”的论文™ Client Compiler” 显示(作为第一个结果)并为我们提供了一些见解:
摘要:
马克·梅奥很好地解释了这一点。
底线:如果 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:
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.
它的工作原理是不断分析程序的性能,寻找可能频繁或重复执行的“热点”,然后针对性能关键性较低的代码进行优化,以最小的开销实现高性能执行。
因此,理论上,如果存在一些边界检查,并且通过重复和频繁的执行可以明显看出它不可能超出边界,那么热点可能会优化这些检查。并不意味着它是绝对正确的,但这可能是它发生的原因之一。
来自 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."