GC finalizer optimization.

Dave Dave_member at pathlink.com
Tue Apr 11 15:38:57 PDT 2006


Dave wrote:
> 
> Argh - I forgot about needing to release synchronization resources or 
> zeroing the vptr. regardless of if there is a dtor or not...
> 
> Damn.
> 

Maybe all is not lost, change gcx.setFinalizer to:

     void setFinalizer(void *p, GC_FINALIZER pFn)
     {
	// should be thread-safe - Threads.nthreads is
	//  mutex'd in std/thread.d
         if(Thread.nthreads > 1)
         {
         synchronized (gcLock)
         {
             gcx.finalizer = pFn;
             gcx.doFinalize(p);
         }
         }
         else
         {
             gcx.finalizer = pFn;
             gcx.doFinalize(p);
         }
     }

Now you get:

Before:
D::~this
C::~this
C::~this
0.827

After:
D::~this
C::~this
C::~this
0.466

- Dave

> Dave wrote:
>>
>> Comments? Like, how can this break things?
>>
>> By changing line 129 of phobos/internal/gc/gc.d from:
>>
>> _gc.setFinalizer(p, &new_finalizer);
>>
>> to:
>>
>> ///_gc.setFinalizer(p, &new_finalizer);
>> ///
>>     ClassInfo c = ci;
>>     do
>>     {
>>         if (c.destructor)
>>         {
>>             _gc.setFinalizer(p, &new_finalizer);
>>         }
>>         c = c.base;
>>     } while (c);
>> ///
>>
>> gives me about 3x performance in allocating class objects w/o a dtor 
>> using the following code.
>>
>> Before:
>> D::~this
>> C::~this
>> C::~this
>> 0.829
>>
>> After:
>> D::~this
>> C::~this
>> C::~this
>> 0.258
>>
>> ;---
>>
>> import std.date, std.stdio;
>>
>> void main()
>> {
>>     C c = new C;
>>     D d = new D;
>>     E e;
>>     F f;
>>
>>     d_time st = getUTCtime();
>>     for(int i = 0; i < 1_000_000; i++)
>>     {
>>         e = new E;
>>         f = new F;
>>     }
>>     d_time et = getUTCtime();
>>     writefln((et - st) / cast(double)TicksPerSecond);
>> }
>>
>> class C
>> {
>>     int i;
>>     ~this()
>>     {
>>         printf("C::~this\n");
>>     }
>> }
>>
>> class D : C
>> {
>>     int i;
>>     ~this()
>>     {
>>         printf("D::~this\n");
>>     }
>> }
>>
>> class E
>> {
>>     int i;
>> }
>>
>> class F : E
>> {
>>     int i;
>> }
>>
>> Thanks,
>>
>> - Dave



More information about the Digitalmars-d mailing list