Robert Klemme schrieb:
> Yes, but the cost is not in the check but in the branching on processor
> level (see what Patricia wrote).
Depends on the processor and on the branch. If
new is just heap -= size, what some papers suggest,
then it might not be important.
But if new is much more, then sure the branch
interrupts the normal code flow so much that
instruction piplining gets out of sync. And
the speed gain by instruction overlapping
is lost.
But my hypothesis is more that something
algorithmically on a higher level happens than
something on the lower hardware level.
So I also found something about "Lock
coarsening"(*), so if the new needs some lock
this lock could be aquired before the initialization
loop and released after the initialization
loop. So that:
Bla[] bla = new Bla[n];
for (int i=0; i<n; i++) {
bla[i] = new Bla();
}
Would only need 1 locking instruction pair,
where as:
Bla[] bla = new Bla[n];
...
if (bla[i]==null)
bla[i] = new Bla();
..
Would need n locking instruction pairs. But I
would still need some confirmation that JITs
are able to do such an optimization on a higher
level in the present case.
I am also not sure whether the bounded for
counts as a looping control flow, so that "Lock
coarsening" would not be applicable. But
I guess it can be deemed non looping, so that
the technique would be applicable.
Currently I am planning to change the loop
to something like that:
Bla[] bla = new Bla[n];
for (int i=0; i<n; i++) {
bla[i] = createBla();
}
So that the JIT has less information on what
the loop is about, and to do some new
measurements. To be fair I would also use
createBla() in the lazy scenario. Lets see
what happens.
Best Regards
(*)
http://java.sun.com/performance/refe...nce.html#2.1.2