Shared keyword and the GC?

Michel Fortin michel.fortin at michelf.ca
Fri Oct 19 07:50:36 PDT 2012


On 2012-10-19 07:42:53 +0000, Jacob Carlborg <doob at me.com> said:

> On 2012-10-19 03:06, Michel Fortin wrote:
> 
>> All this is nice, but what is the owner thread for immutable data?
>> Because immutable is always implicitly shared, all your strings and
>> everything else that is immutable is thus "shared" and must be tracked
>> by the global heap's collector and can never be handled by a
>> thread-local collector. Even if most immutable data never leaves the
>> thread it was allocated in, there's no way you can know.
>> 
>> I don't think per-thread GCs will work very well without support for
>> immutable data, an for that you need to have a distinction between
>> immutable and shared immutable (just like you have with mutable data). I
>> complained about this almost three years ago when the semantics of
>> shared were being defined, but it got nowhere. Quoting Walter at the time:
> 
> Would it be any difference if the immutable data was collected from a 
> different collector than the shared or thread local?
> 
> In this case I guess the collector wouldn't try to make a difference 
> between shared and non-shared immutable data.

A thread-local GC would be efficient because it scans only one thread. 
The gain is that you minimize the load on the global GC, reducing 
collection cycles that need to stop all threads. Creating a second 
global GC for immutable data wouldn't free you from the need to stop 
all threads, but now you'd have two global collectors stopping all 
threads which would probably be worse. So I don't see the point in a 
second GC for immutable data.

Immutable data must always be handled by a global GC. There's no way 
around it as long as immutable and shared-immutable are the same thing. 
The more immutable data you have, the more irrelevant the performance 
gains from a thread-local GC becomes, because it get used less often. 
This creates a strange incentive to *not* make things immutable in 
order make things faster.

I'm all for a thread-local GC, but in the current state of the type 
system it'd just be ridiculous. But then, perhaps an implementation of 
it could convince Walter to change some things. So if someone is 
inclined to implement it, go ahead, I'm not here to stop you.

-- 
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca/



More information about the Digitalmars-d mailing list