Array bounds checks
Bryce McKinlay
bryce@albatross.co.nz
Thu Oct 26 01:39:00 GMT 2000
I was wondering why using "--no-bounds-checks" makes such a big
difference to the performance of a lot of array-heavy code. In a simple
example like the following:
ABC()
{
int[] ia = new int[24];
for (int i=0; i < ia.length; i++)
{
ia[i] = 32;
}
}
Should not the optimizer be able to eliminate the bounds check, or at
least move it out of the inner loop? Basically this loop expands to:
for (int i=0; i < ia.length; i++)
{
if (i > ia.length) throw ArrayBoundsException;
ia[i] = 32;
}
So it seems pretty clear to me that the (i > ia.length) condition can
never be true, especially considering that I'm not making any calls or
doing anything that could change the value of ia, So why can't the
optimizer remove it? This is a snippit of the .s output emitted using
-O2:
call _Jv_NewArray
movl %eax, %ebx
xorl %eax, %eax
addl $16, %esp
cmpl 8(%ebx), %eax
jge .L10
leal 12(%ebx), %ecx
.p2align 4
.L5:
movl 8(%ebx), %edx
cmpl %edx, %eax
jae .L11
movl $32, (%ecx)
incl %eax
addl $4, %ecx
cmpl %edx, %eax
jl .L5
.L10:
movl -4(%ebp), %ebx
movl %ebp, %esp
popl %ebp
ret
.p2align 4,,7
.L11:
subl $12, %esp
pushl %eax
call _Jv_ThrowBadArrayIndex
regards
[ bryce ]
More information about the Java
mailing list