On Tue, Oct 20, 2009 at 10:05 AM, Andrei Alexandrescu
<seewebsiteforem...@erdani.org> wrote:
> Bill Baxter wrote:
>>
>> To Andrei, do you really feel comfortable trying to explain this in
>> your book?  It seems like it will be difficult to explain that ~= is
>> sometimes efficient for appending but not necessarily if you're
>> working with a lot of arrays because it actually keeps this cache
>> under the hood that may or may not remember the actual underlying
>> capacity of the array you're appending to, so you should probably use
>> ArrayBuilder if you can, despite the optimization.
>
> I guess I'll try and let you all know.

I can also see this becoming an Effective D tip --
"""
#23  Use ArrayBuilder for appending

For common cases appending to slices is fast.  However the performance
depends on a hidden LRU cache to remember the capacities of the most
recent N arrays.  This works fine until you hit that N limit.
Unfortunately as you compose code together it is easy to overflow that
cache without realizing it, leading to sudden performance drops for no
apparent reason.   Thus we suggest you always use ArrayBuilder when
appending to arrays rather than slices.
"""

Or not.  This is one of those places where some data is really needed.
 It may be that 99.9% of code is only actively appending to 4 arrays
or fewer.  It just seems too tricky that this innocent-looking code:

     int[] i;
     foreach(k; 1..20_000) {
             i ~= some_function(k);
     }

could hit a performance cliff based on how many arrays get used deep
in the call chain of some_function().   Granted, cache issues can
cause these kinds of cliffs for any kind of code, but I suspect this
cliff would be particularly noticeable, given the slowness of
allocations.

--bb

Reply via email to