Change representation of dynamic arrays?

Graham St Jack grahams at acres.com.au
Sun Oct 21 20:32:11 PDT 2007


David Brown wrote:
> On Mon, Oct 22, 2007 at 09:55:12AM +0930, Graham St Jack wrote:
> 
>> The extra pointer could be used by the concatenation operators to 
>> dramatically reduce the number of reallocs that occur when doing ~= 
>> appends to a dynamic array - by only realloc-ing when capacity runs 
>> out, and allocating more capacity than immediately needed.
> 
> D already makes use of this information in the GC.  In fact, allocations
> are always powers of 2, up until the page size, and then in full pages.
> Try the following:
> 
> import std.stdio;
> import std.gc;
> 
> void main ()
> {
>     char[] text;
> 
>     for (int i = 0; i < 256; i++) {
>         text ~= 'a';
>         writefln ("len = %d, alen = %d", text.length, capacity (text.ptr));
>     }
> }

Cool - thanks.



More information about the Digitalmars-d mailing list