May 20, 2004
I'm having some trouble making a custom allocator. I need it to be very fast because this particular class will have instances created and destroyed rapidly. Here is the test class:


class Foo
{
 private static byte[] buf;
 private import std.gc;

 new(uint sz)
 {
  void* result;

  // synchronized
  {
   if(sz > buf.length)
    buf = new byte[100 + sz];

   result = buf[0 .. sz];
   buf = buf[sz .. buf.length];
  }

  // std.gc.addRange(result, result + sz); // So that it can contain
pointers.
  return result;
 }

 delete(void* p)
 {
  printf("delete %p\n", p);

  // std.gc.removeRange(p);
 }
}


Besides the fact that the synchronized block slows the function down considerably, it causes the program to hang when main returns. It needs to be synchronized, right? My code is multithreaded. Or is anything with the GC automatically synchronized?

I commented out the addRange() call in new() because it causes any
allocations' speed to degrade over time, I think because delete() is never
being called (bug?) to removeRange(), resulting in a large table to scan. I
think addRange() is needed, though not with the current GC.

Here's what I'm using to test the speed:


import std.date;

int main()
{
 for(uint n = 0; n != 10; n++)
 {
  d_time before;

  before = getUTCtime();
  for(uint i = 0; i != 100_000; i++)
  {
   Object o = new Object;
   // delete o;
  }
  printf("Object: %d ticks.\n", cast(int)(getUTCtime() - before));

  before = getUTCtime();
  for(uint i = 0; i != 100_000; i++)
  {
   Foo f = new Foo;
   // delete f;
  }
  printf("Foo: %d ticks.\n", cast(int)(getUTCtime() - before));

  std.gc.fullCollect();
  std.gc.minimize();
 }

 return 0;
}


Thanks. - Chris.


May 21, 2004
> Besides the fact that the synchronized block slows the function down considerably, it causes the program to hang when main returns.

had the same problem. its weird :p iirc u must synchronize upon an object like 'synchronized(bar)', where bar is your critical section / mutex.