Reserving capacity in associative arrays
Jon D via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Feb 16 14:02:29 PST 2016
On Tuesday, 16 February 2016 at 19:49:55 UTC, H. S. Teoh wrote:
> On Tue, Feb 16, 2016 at 07:34:07PM +0000, Jon D via
> Digitalmars-d-learn wrote:
>> On Tuesday, 16 February 2016 at 16:37:07 UTC, Steven
>> Schveighoffer wrote:
>> >On 2/14/16 10:22 PM, Jon D wrote:
>> >>Is there a way to reserve capacity in associative arrays?
>> >>[snip]
>> >>The underlying implementation of associative arrays appears
>> >>to take
>> >>an initial number of buckets, and there's a private resize()
>> >>method,
>> >>but it's not clear if there's a public way to use these.
>
> Rehashing (aa.rehash) would resize the number of buckets, but
> if you don't already have the requisite number of keys, it
> wouldn't help.
>
Thanks for the reply and the detailed example for manually
controlling GC. I haven't experimented with taking control over
GC that way.
Regarding reserving capacity, the relevant method is aa.resize(),
not aa.rehash(). See:
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/aaA.d#L141. This allocates space for the buckets, doesn't matter if the keys are known. Note that every time the buckets array is resized the old bucket array is walked and elements reinserted. Preallocating allocating a large bucket array would avoid this. See also the private constructor in the same file (line 51). It takes an initial size.
--Jon
More information about the Digitalmars-d-learn
mailing list