draft proposal for ref counting in D

Walter Bright newshound2 at digitalmars.com
Wed Oct 9 19:29:14 PDT 2013


On 6/30/2013 5:25 PM, Steven Schveighoffer wrote:
 > On Jun 30, 2013, at 8:18 PM, Walter Bright wrote:
 >
 >> I very much want to avoid requiring atomic counts - it's a major performance 
penalty. Note that if the GC is reaping a cycle, nobody else is referencing the 
object, so this should not be an issue.
 > I think you didn't understand what Michel was saying.
 >
 > Take for example:
 >
 > A->B->C->A
 >
 > this is a cycle.  Imagine that nobody else is pointing at A, B or C.  Fine. 
The GC starts to collect this cycle.
 >
 > But let's say that D is not being collected *AND* B has a reference to D.
 >
 > B could be getting destroyed in one thread, and decrementing D's reference 
count, while someone else in another thread is incrementing/decrementing D's 
reference count.
 >
 > I agree that RC optimally is thread-local.  But if you involve the GC, then 
ref incs and decs have to be atomic.

This is actually a problem right now with the GC, as destructors may be run in 
another thread than they belong in. The situation you describe is not worse or 
better than that, it's the same thing. The solution is to run the destructors in 
the same thread the objects belong in.

 >
 > I don't think this is that bad.  iOS on ARM which has terrible atomic 
primitives uses atomic reference counts.

It's bad. ARM is not the only processor out there.


More information about the Digitalmars-d mailing list