A question/suggestion regarding the GC

Jarrett Billingsley jarrett.billingsley at gmail.com
Sat Sep 20 13:07:31 PDT 2008


On Sat, Sep 20, 2008 at 3:04 PM, bearophile <bearophileHUGS at lycos.com> wrote:
> In \phobos\internal\gc\gc.d  there's this function:
>
> void setTypeInfo(TypeInfo ti, void* p) {
>  if (ti.flags() & 1)
>    hasNoPointers(p);
>  else
>    hasPointers(p);
> }
>
> At first sight the code of that function looks wrong, because if you call it like this:
>
> struct S { int* pi, int i }
> setTypeInfo(typeid(S), void* p);
>
> then the GC will look for pointers in the 'i' field too. So setTypeInfo() becomes nearly useless to me and I can use my HasReferences!() template and call hasNoPointers/hasPointers manually.

Congratulations, you've figured out how conservative GCs work.

> TypeInfo knows at runtime the name of the type, so at runtime it can use an associative array to find the name mangling of the given type, and with that it can determine if each field of S is a pointer/reference or not (and it can even store such information into a bitarray for future needs, so the name demangling is done only once, and only for types that receive a typeid() call on them). This isn't perfect because it may mark as reference pointers to not-GC-managed memory, but it look better than the current situation anyway.

This information would be embedded in the typeinfo at compile time,
not figured out at runtime.  Geez, if you're looking for
performance...

But the number of pointers to non-GC'ed memory is so minimal that it's
most likely not a problem.

> I think this isn't going to slow down D code. And can make the GC a bit more precise. I think this may help speed up the deallocation/management of associative arrays too.

D can never have a precise GC because of unions, sadly.  But SafeD
could have a precise GC.

The performance effects, however, are a bit more complicated than "I
think it's not going to be a problem."  You have a way with being
unconvincing in your proposals.


More information about the Digitalmars-d-learn mailing list