DIP60: @nogc attribute

via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 22 02:40:13 PDT 2014


On Tuesday, 22 April 2014 at 09:01:20 UTC, Walter Bright wrote:
> On 4/21/2014 11:51 PM, "Ola Fosheim Grøstad" 
> <ola.fosheim.grostad+dlang at gmail.com>" wrote:
>> This is actually quite efficient compared to the standard 
>> NSObject which uses a
>> hashtable for refcounting:
>
> It's not efficient compared to pointers.

It isn't efficient compared to pointers if you use a blind ARC 
implementation. If you use ARC to track ownership of regions then 
it can be efficient.

The real culprit is multithreading, it can be resolved though. If 
you put the counters on cachelines that are local to the thread, 
either by offset or TLS.

E.g. pseudocode for 8 refcounted pointers on 4 threads with 32 
bytes cachelines could be something along the lines of:

struct {
    func* destructor[8]; // cacheline -1
    void* ptr[8]; //cacheline 0
    uint bitmask[8]; //cacheline 1
    int refcount[8*4] ; //cacheline2-6 initialized to -1
}

THREADOFFSET = (THREADID+2)*32

retain(ref){ //ref is a pointer to cacheline 0
  if ( increment(ref+THREADOFFSET) == 0) {
    if( CAS_SET_BIT(ref+32,THREADID)==THREADID ){
       HALT_DESTRUCTED()
    }
  }
}

release(ref){
   if( decrement(ref+THREADOFFSET)<0 ){
     if( CAS_CLR_BIT(ref+32,THREADID)==0){
        call_destructor(ref-32,*ref);
     }
   }
}



More information about the Digitalmars-d mailing list