Fast temporary dynamic arrays? (And slicing of them)

Tom Kazimiers 2voodoo at gmx.de
Tue Sep 7 08:46:30 PDT 2010


Hi,

On 09/06/2010 04:55 AM, Jonathan M Davis wrote:
> Static arrays are value types, but dynamic arrays are reference types.
>
> [...]
>
> No array copying takes place anywhere in that program. If you want to
> copy an array, you'd do one of the following
>
> [...]
>
> Passing dynamic arrays to functions passes the reference.

Thanks for your clarification and examples, that made the whole array
handling clearer to me.

On 09/07/2010 02:56 PM, Steven Schveighoffer wrote:
> On Sun, 05 Sep 2010 22:41:50 -0400, bearophile wrote:
>> Tom Kazimiers:
>>> How can I have a (temporary) dynamic array on stack and make
>>> references to it (no copying)? I successively put integers in an
>>> array (but don't know how much there will be in advance) with an
>>> appender!(int[]) and get the date out with appender.data(). Later
>>> on I pass the result to a method as an "in int[]" parameter. Is
>>> that already a reference or will it be copied? Are there better
>>> methods to accomplish this? The method receiving such an array will
>>> not modifiy contents of the array, but only read from it.
>>
>> The appender.data() doesn't currently copy data.
>>
>> There is no standard way to put a growable array on the stack. Maybe
>> you can hack it with several successive calls to alloca(), but I have
>> never tried it.

Ok, good to know. But for now I will stay away from optimizations with
alloca, etc. - its's harder to read the code then - premature
optimization is evil :-)

> Hm... you can do something like this (after upcoming release, appender
> has changed):
>
> void foo()
> {
>    int[1024] buf;
>    auto app = appender(buf[]);
>    app.clear();
>    ...
> }
>
> After app.clear, appender will fill up the static buffer until full,
> and then reallocate on the heap.
>
> Note that the new appender uses heap data to store its implementation,
> so it's not as quick as it could be.  This is per Andrei's requirement
> that it be a reference type.

That sound good - when will this upcoming release be?

Cheers,
Tom


More information about the Digitalmars-d-learn mailing list