Fast GC allocation of many small objects

Boris-Barboris ismailsiege at gmail.com
Sat Mar 31 09:10:13 UTC 2018


On Friday, 30 March 2018 at 20:31:35 UTC, Per Nordlöw wrote:
> Is there a faster way of allocating many small class objects 
> such as...

maybe something like this:

import std.conv: to;
import std.stdio;

class Node {}

class StrNode : Node
{
     string value;
}

void main()
{
     writeln(StrNode.classinfo.name);	// onlineapp.StrNode
     size_t il = StrNode.classinfo.m_init.length;
     writeln(il); // 32
     void[] backBuf = new void[il * 1000];
     StrNode[] nodes = new StrNode[1000];
     for (int i = 0; i < 1000; i++)
     {
         backBuf[i * il .. (i+1) * il] = StrNode.classinfo.m_init;
         nodes[i] = cast(StrNode) &backBuf[i * il];
         nodes[i].value = i.to!string;
     }
     foreach (n; nodes[995..$])
         writeln(n.classinfo.name, " ", n.value);	
         // prints onlineapp.StrNode 995-999
}


More information about the Digitalmars-d-learn mailing list