Pre-expanding alloc cell(s) / reserving space for an associative array

Steven Schveighoffer schveiguy at gmail.com
Mon Jul 10 02:05:50 UTC 2023


On 7/9/23 4:24 PM, 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.

No there is no such `.reserve` call for associative arrays.

It might be possible to implement, but it's quite a bit different from a 
normal array reserve -- an associative array allocates all its elements 
as individual memory blocks, so there is no place to reserve things. You 
would have to add e.g. a free-list, that is reserved from the allocator 
itself.

-Steve


More information about the Digitalmars-d-learn mailing list