Re: Bulk Array Element Allocation, is it faster?
On 9/25/2011 4:47 AM, Jan Burse wrote:
Roedy Green schrieb:
new does two things:
1. allocates X bytes of space
2. initialises that block of space to a pattern.
So a dumb translation of the loop would be:
1. for each i between 0 and n-1
1.1 allocates X bytes of space
1.2 call the initializer
Then I was more thinking that the loop does the
following, i.e. is cleverly compiled to:
1. allocates n*X bytes of space
2. for each i between 0 and n-1
2.1 call the initializer for location X*i
At least I would hand assembler it like this.
Saving n-1 calls to the heap allocator.
Since the heap allocator probably consists of one compare-and-
branch ("Is there enough room?") and one addition ("Advance the
nextFreeAddress pointer"), the savings are unlikely to be large.
This isn't malloc(), you know.
I was already googling a little bit about the
compilation techniques found for JIT today, but
did not yet hit any evidence for clever loop
allocation, but it is so obvious I really it
must be done. But maybe it is not done because
of multi-threading or GC, who knows...
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. ;-)
--
Eric Sosman
esosman@ieee-dot-org.invalid