Associative Array potential performance pitfall?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Mar 13 14:41:03 UTC 2020


On Fri, Mar 13, 2020 at 09:30:16AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote:
> On 3/13/20 5:24 AM, H. S. Teoh wrote:
> > Note that `arg ~ arg` may allocate, but it also may not if the
> > current buffer for `arg` is big enough to accomodate both.
> 
> That always allocates. Only appending may avoid allocation:
> 
> arg ~= arg;

Ah I see.  Mea culpa.


> But, I would instead use ranges if possible to avoid all allocations.
[...]

Initially I thought about avoiding all allocations, but I realized that
no matter what, you need to allocate the key for the AA anyway, so I
didn't go that route.  That said, though, we could probably reduce the
number of allocations by various means, such as having the function
return char[] instead of string, and calling .idup on-demand as opposed
to allocating up-front. This would allow us to reuse a static buffer for
`repeated` instead of incurring an allocation each time.  Given what the
algorithm is doing, this should save some number of allocations if done
correctly.


T

-- 
Creativity is not an excuse for sloppiness.


More information about the Digitalmars-d-learn mailing list