On Sunday, 9 July 2023 at 20:24:24 UTC, Cecil Ward wrote:
Before I posted a question about avoiding unnecessary allocs/reallocs when adding entries to an array like so
    uint[ dstring ]  arr;

when I build it up from nothing with successive insertions.

The array is accessed by a key that is a dstring. I was told that I can’t use .reserve or the like on it? Is that correct? My memory fails me, powerful pain drugs.

Is there an alternate method I could use ? To be honest, the number of entries is likely to be extremely modest, so this is not a huge performance issue, six entries would be considered quite a lot. The string keys are probably not very very long. But I’m learning good habits for the day when I might have a bigger such database.

From the spec it sounds as though (but good luck testing for sure) that if you have (for example) 6 big dummy key-value pairs in the AA to begin with, then if you use `.clear` it "Removes all remaining keys and values from [the] associative array. The array is not rehashed after removal, __to allow for the existing storage to be reused.__" ([source](https://dlang.org/spec/hash-map.html#properties)) I'm not 100% sure this would work how you want, and it's such a hack, but it's something. D has a few user-made hash-map libraries, so it might be worth investigating those to see if they offer a pre-allocation method, too.

Reply via email to