Array bounds checks

Cedric Berger cedric@wireless-networks.com
Thu Oct 26 12:11:00 GMT 2000


"Boehm, Hans" wrote:
> 
> > Some compilers generate code twice:
> >
> >    if (upper < array.length)
> >      for (int i = 0;  i < upper; i++)
> >         ... no bounds checking ...
> >    else
> >      for (int i = 0;  i < upper; i++)
> >         ... with bounds checking ...
> >
> > I don't particularly care for this idea ...
> >
> My impression was that you could get more mileage out of this approach by
> also checking for other properties when you decide which version to use.  In
> particular multianewarray should potentially be able to tag the arrays it
> allocates as rectangular and contiguously allocated, and you should be able
> to check for that case here, thus reducing multidimensional array access
> back to the Fortran case (at the risk of retaining more memory than needed).

When you want performance with with rectangular arrays (for example for a
high perf vector/matrix library), it's usually alot better to use a *single*
array:
  class Matrix { 
     final double get(int i, int j) { return array[i*size+j]; }
  }

Cedric


More information about the Java mailing list