Change representation of dynamic arrays?

David Brown dlang at davidb.org
Sun Oct 21 19:50:31 PDT 2007


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));
     }
}



More information about the Digitalmars-d mailing list