Often repeated array allocations

bearophile bearophileHUGS at lycos.com
Sat Jul 20 16:02:56 PDT 2013


Namespace:

> So, you would never allocate with float[]?

Generally in D I allocate on the heap with new, and once in a 
while with minimallyInitializedArray. Stack-allocation is left 
for situations where max performance is needed. Like you have to 
build a tree of string distances, and you use a Levenshtein 
distance to compute them. Usually a Levenshtein distance needs 
one buffer, or two. If you want to put one million of strings in 
such tree of distances, you can't allocate one billions of 
buffers, it's too much wasted time. For such case I allocate the 
Levenshtein buffer with an alloca (or I allocate the buffer 
before the function, etc, there are many ways).

I think the usual D code uses too much heap allocation. 
Allocations on the heap are slow. If you take a look at modern 
Ada code (like Ada2012 code) you see very little heap 
allocations, or no heap allocations at all :-) Probably Rust code 
will avoid lot of of heap allocations.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list