next up previous contents index
Next: Managing Expectations Up: Multiarrays Previous: Sections   Contents   Index

Checking Subscripts

If the extent of a multiarray in one of its dimensions is $N$, then any subscript $i$ in that dimension should be in the range $0, \ldots, N-1$. It would be reasonable to expect that if a subscript in any dimension is out of range then some exception--most likely a java.lang.IndexOutOfBoundsException--would automatically be thrown.

It turns out that this expectation is wrong. One of the goals of HPJava is performance, and the luxury of this level of checking would come at a cost in performance. What actually happens is that any element of a multiarray has a corresponding linear index in a notional underlying, flattened, 1-dimensional array1.5. The formula for the linear index can be worked out easily enough given that the last index is ``most rapidly varying''--we won't bother to reproduce the math here. A java.lang.IndexOutOfBoundsException exception is thrown only if the linear index corresponding to the set of subscripts specified in the program is outside the allowed set of linear indexes for the whole multiarray (or parent multiarray, in the case of a section). If a subscript is out of range for its dimension, but the associated linear index is not out of range for the whole multiarray, the program silently accesses an unexpected element of the (parent) multiarray.

This is a compromise between ease of debugging and performance. Perhaps a future HPJava compiler will have an optimizer clever enough to make the more transparent form of bounds checking feasible, but we are not there quite yet.


next up previous contents index
Next: Managing Expectations Up: Multiarrays Previous: Sections   Contents   Index
Bryan Carpenter 2003-04-15