Fast GC allocation of many small objects

Alexandru Jercaianu alex.jercaianu at gmail.com
Fri Mar 30 23:09:33 UTC 2018


On Friday, 30 March 2018 at 20:46:43 UTC, Per Nordlöw wrote:
> On Friday, 30 March 2018 at 20:38:35 UTC, rikki cattermole 
> wrote:
>> Use a custom allocator (that could be backed by the GC) using 
>> std.experimental.allocators :)
>
> https://dlang.org/phobos/std_experimental_allocator.html
>
> is massive.
>
> I guess I should allocate my nodes using
>
>     auto node = theAllocator.make!StrNode("alpha");
>
> Could someone please give a working example of a GC-backed 
> `theAllocator` suitable for my allocation pattern?

Hello,

You can try the following:
     struct Node
     {
         char[64] arr;
     }

      enum numNodes = 100_000_000;
      void[] buf = GCAllocator.instance.allocate(numNodes * 
Node.sizeof);
      auto reg = Region!(NullAllocator, 16)(cast(ubyte[])buf);

      foreach(i; 0 .. numNodes)
      {
          Node* n = cast(Node*)(reg.allocate(Node.sizeof).ptr);
          // do stuff with the new node
      }

I benchmarked this versus
      foreach(i; 0 .. numNodes)
      {
          auto n = new Node;
          // do stuff...
      }

The first approach was about 30% faster.


More information about the Digitalmars-d-learn mailing list