mozilla / pluotsorbet

[ARCHIVED] PluotSorbet is a J2ME-compatible virtual machine written in JavaScript.
GNU General Public License v2.0
237 stars 46 forks source link

Use throw instead function calls for exceptions. #1860

Closed brendandahl closed 6 years ago

brendandahl commented 9 years ago

Mbx and I found this when running the bubble sort benchmark. Seems to do nothing for startup time, but does improve benchmarks from 8600ms to 7900ms on my machine. Also improves bubble sort bench quite a bit if more rounds are run.

mbebenita commented 9 years ago

I think what is happening is that function calls in loops (even though they are never called) prevent some optimizations from happening.

This Java code compiles to:

for (int i = 0; i < 10000; i++) {
  a[i] = i;
}
for (int i = 0; i < 10000; i++) {
  !a&&TN();
  if((i>>>0)>=(i32[a>>2]>>>0))TI(i);
  a[i] = i;
}

I think the presence of TN and TI prevent some optimizations, otherwise it doesn't really explain why the new patch is faster. This is now:

for (int i = 0; i < 10000; i++) {
  if(!a)throw 0;
  if((i>>>0)>=(i32[a>>2]>>>0))throw 1;
  a[i] = i;
}

Where 0 means a null pointer exception, and 1 is an array index out of bounds exception.

brendandahl commented 9 years ago

What do we do about the index? Also, what do we do about negative array indices? Is that handled correctly down the line?

There's a constructor for the exception without the index. Negative array indices are array bounds exceptions, creation with a negative array length is the NegativeArraySizeException.

mykmelez commented 9 years ago

@mbebenita Are you satisfied with this change now?

mykmelez commented 6 years ago

I'm closing this pull request (and all others) as this project is no longer active.