Re: Bulk Array Element Allocation, is it faster?
On Sunday, September 25, 2011 10:21:42 AM UTC-7, Jan Burse wrote:
Eric Sosman schrieb:
Note that the semantics of the bulk and lazy allocations
in your original example are quite different. An "optimization"
that changes the meaning of the code is only a benefit if the
code was wrong to begin with. ;-)
What I do in the code has nothing to do with my question. My
It has a great deal to do with your question.
question circles only around the following code fragment:
Bla[] bla = new Bla[n];
Here the system does allocate, as you plaintively request, "n*X bytes of sp=
ace", where "X" is the size of a pointer.
for (int i=0; i<n; i++) {
bla[i] = new Bla();
}
How would you imagine the JIT would optimize this without taking semantics =
into account? Each 'Bla' is required to be independently GC-able, you know=
.. The semantics of 'Bla[]' is not a block of instances; it's a block of re=
ferences.
How would you bulk allocate a block of references each to a different refer=
ence? About the only way is to add a fixed offset to the value pushed into=
each successive array element, but that's already what you are doing with =
'new' anyway, so you aren't "optimizing" by doing the same thing.
If the 'Bla' constructor is heavyweight, the construction of your n 'Bla' i=
nstances will far outweigh any slight overhead in calculating that offset i=
ncrease for each array element.
I just don't see how you expect to bulk-load n different pointers into that=
array. Could you explain that in more detail, please?
And what the JIT does. And not what I am doing in a comparison
with lazy. That is why I started my post with:
"I just wonder wether [sic] modern JIT [sic] do optimize
Code [sic] of the following form:"
Please focus on that.
Aye, aye, focusing on that, Captain!
The answer is "nothing", because the semantics of the operation are such th=
at the current mechanism is already quite close to optimal. This is what p=
eople have been explaining to you, that you dismissed as irrelevant.
--
Lew