"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.
When your program is running and it tries to access an element of an array, the Java virtual machine checks that the array element actually exists. This is called bounds checking.
As a Java program is running, each time an array index is used it is checked to be sure that it is OK. This is called bounds checking, and is extremely important for catching errors.
Array bound checking refers to determining whether all array references in a program are within their declared ranges.
Simply use: boolean inBounds = (index >= 0) && (index < array. length); Implementing the approach with try-catch would entail catching an ArrayIndexOutOfBoundsException , which is an unchecked exception (i.e. a subclass of RuntimeException ).
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.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With