Array bounds checks

Tom Tromey tromey@cygnus.com
Thu Oct 26 10:52:00 GMT 2000


Per> Some compilers generate code twice:
Per> [ ... ]
Per> I don't particularly care for this idea ...

Another approach is to modify the bounds:

  __upper = min (upper, array.length)
  for (int i = 0; i < __upper; i++)
    ... no bounds checking ...
  for (; i < upper; i++)
    ... bounds checking ...

This gives better performance.  I guess you don't even need a second
loop, just a copy of the body with bounds checking enabled.

Per> But as for the questions why the compiler cannot optimize away
Per> the bounds check in your example, I don't know.  I thought there
Per> was code in gcc to do some range analysis and bounds check
Per> removal.

My impression is that the value range code only handles constants, not
things like `array.length'.  I recommend asking on the gcc list to get
the real answer though.

Tom


More information about the Java mailing list