views:

575

answers:

1

Now that Java is open source, one of the first things I would like to do is to disable array bounds checking for certain blocks of code, where I'm dead sure I cannot go offbounds and where performance heavily matters.

Now, I'm not a compilers/grammar expert, so any syntax would be good enough for me: Here's one that I can think of:

pragma_disable_array_bounds_checking_begin
  for(x = xMin; x < xMax; x += xIncr) {
    for(y = yMin; y < yMax; y += yIncr) {
      for(z = zMin; z < zMax; x += zIncr) {
        sample_and_draw(voxel[x][y][z]);
      }
    }
  }
pragma_disable_array_bounds_checking_end

I understand that after this change, my local version will cease to be Java. But I care less since I can always bundle the recompiled VM along with my app.

I don't know how to go about making this nontrivial change, hence the question. Note that I'm not interested in the JNI approach.

+1  A: 

If you can tell that the array index cannot go out of bounds, perhaps the compiler can too, they've gotten pretty impressive in static data-flow and escape analysis recently. I'd check first to see if that bounds-check is even being performed. javap can help you here.

Of course, I'm assuming you're following Knuth's advice of not optimizing prematurely, and that you've measured your application and determined that this difference is worth spending time on. If so, please post a summary of your results, as this would be very interesting to me. even if my apps would not benefit from such tweaking.

Dov Wasserman
Thanks for writing.Please see this link (especially, the last few lines of page 1 and the first few lines of page 2):http://www.cg.tuwien.ac.at/research/publications/2007/Kohlmann-2007-EBV/Kohlmann-2007-EBV-paper.pdf
I'm 99% sure that the JVM can figure this out and generate optimal code. You can alternatively try using foreach (http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html), but this seems a hard to justify microoptimization.
Cd-MaN
All, I would still like to know how to disable array bounds checking, regardless of the problem at hand.