draft proposal for ref counting in D

deadalnix deadalnix at gmail.com
Mon Oct 14 19:20:49 PDT 2013


On Tuesday, 15 October 2013 at 00:38:20 UTC, Michel Fortin wrote:
> So as I understand it, your plan would then be:
>
> - use concurrent GC using the page protection mechanism and COW 
> to catch writes during collection
> - add thread-local awareness to the GC so only shared and 
> mutable memory needs COW
>

Yes, I think this is what make the most sense for D. If the GC 
allow explicit free; the reference counting and other manual 
memory management techniques can be built on top of it.

> Makes sense. But adding thread-local awareness requires a 
> couple of language changes. It won't work as long as people 
> keep casting things around so you need to fix a lot of cases 
> where casts are needed.
>

Type qualifiers provide the necessary information.

However, some practice (that are already mentioned as being 
undefined behavior) will become really unsafe. It also require to 
enrich the GC api, but users aren't supposed to use it directly :D

> I'm a little wary of the cost of doing COW during collection. 
> Obviously the GC isn't pausing the program per-see, but it'll 
> be slowing it down. By how much is probably dependent on what 
> you're doing. At the very least the GC should allocate types 
> with no pointers on separate pages from those with pointers.
>

Nothing is free, and indeed, trapping the write via memory 
protection is quite expensive. Hopefully, we can segregate object 
that contain mutable pointer from others and only memory protect 
theses.

It will indeed cause trouble for code that mutate a large amount 
of shared pointers. I'd say that such code is probably asking for 
trouble in the first place, but as always, no silver bullet. I 
still think solution is the one that fit D the best.

> Also, what are the calls required to implement page protection 
> and COW on posix? I'd like to check whether those are allowed 
> within the OS X and iOS sandbox. For instance fork() isn't 
> allowed for sandboxed apps.

You need mmap, mprotect and all the signal handling machinery.


More information about the Digitalmars-d mailing list