AA clear property

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 6 04:11:07 PST 2011


On Sunday, November 06, 2011 13:39:27 Alex_Dovhal wrote:
> "Marco Leise" <Marco.Leise at gmx.de> wrote:
> > No way I heard of. You could delete the entries one by one. :D
> > I guess setting all references to null is the easiest way to 'clear' an
> > AA.
> 
> Thanks. I tried both of them in small benchmark. Results are
> (1) fill_values(aa); aa = null
> (2) fill_values(aa); aa.remove(...)
> For both methods time1/time2 is around 35/52, with aa size 10, 100, 1000,
> and 10000.
> Interesting enough, with aa size 1M, 10M, 100M time1/time2 is around 1/1.
> So (1) is prefered for 10-1000 elements situation i have, but it allocates
> memory each time :( while 2 reuses it, IMHO.

2 doesn't necessarily reuse it. It depends on what the GC does and how AAs are 
currently implemented. It's likely that in both cases, you end up with a fair 
bit of stuff which is on the heap and no longer referenced such that if a 
garbage collection cycle runs, it would be collected (or if one doesn't run, 
it just sits there still taking up memory). The only real difference is that 
_some_ of the memory will be reused with 2, whereas with 1, you need a whole 
new AA. In both cases, new memory will be required. How much that is depends n 
the current implementation of AAs. And which one is more likely to trigger a 
GC cycle probably depends on a number of factors. So, it wouldn't suprise me 
at all either way. I imagine that it's at least simpler for the GC to collect 
the memory quickly in the first case, since the entire AA can be collected 
instead of all of it, but I don't know.

AAs are a _completely_ different situation from dynamic arrays. With dynamic 
arrays, you have a clear block of contiguous memory that you're playing with, 
and it's obvious what's going to be reused if you want reuse. The situation is 
_far_ more complicated with a hash table, which is likely to have a fair 
number of items on the heap - particularly for handling each bucket (and 
there's a decent chance that each of those is a dynamic array). So, I very 
much doubt that your going to reuse much memory from an AA unless a GC cycle 
is run, regardless of whether you do #1 or #2.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list