[Issue 2504] Reserve for associative arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Sep 23 01:38:44 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2504


ZY Zhou <rinick at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rinick at gmail.com


--- Comment #1 from ZY Zhou <rinick at gmail.com> 2009-09-23 01:38:43 PDT ---
(In reply to comment #0)
> It appears that adding an element to an associative array always triggers a
> memory allocation.  Especially in multithreaded code, this is inefficient.  It
> would be nice if associative arrays had a .reserve(size_t n) property, which
> reserved enough space for n objects, and stored the capacity internally.  The
> idea is that, until the reserve buffer is exhausted, no interaction of any kind
> with the GC would be needed to add an element to the AA.

It's not all about memory

int[int] a;
foreach(i;0..20_000_000) a[i] = i; //takes 2 _minutes_
foreach(i;0..20_000_000) a[i] = i; //do it again, only 2 seconds

60 times slower.
If it's memory allocation problem, the result should be similar to Dynamic
array:

int[] t;
foreach(i;0..20_000_000) t ~= i; // 2 seconds
foreach(i;0..20_000_000) t[i] = i; // 0.3 seconds

I guess AA rehashs the whole array when capacity changes, and rehashing is much
slower than memory allocation.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list