Circular refs

David A. Cuthbert dacut at kanga.org
Fri Oct 1 00:23:39 EDT 1999


Tim Peters wrote:
> [David A. Cuthbert]
> > ...
> > I've also toyed with the idea of replacing the Python gc code with
> > a Lins collector (which is based on reference counting but has the
> > ability to reclaim cycles).
> 
> You would hit bad problems with this (I did <0.5 wink>).  Such as: (a) you
> can't color reclaimed cells (Python doesn't pretend to own memory, and
> happily accepts PyObject* pointers from external modules -- the type's
> destructor decides what to do with reclaimed cells, and Python never assumes
> it can carry info in a dead external object);

The (admittedly cheesy) workaround here is: all PyObjects must be allocated
on the heap, and they must be allocated using a specific allocator.  This
also means that external modules must be rewritten.  Not pretty, I'll admit,
but I've had this solution forced on me when using C++ in some environments.

(I realize that I'm neglecting the whole issue of external types here...
that's yet another whole can of worms and spilt milk that'll just clog up
the kitchen sink that we burned before we crossed it. :-)

> (b) even adding two bits for a
> color field actually adds 4 bytes to each object, due to structure
> alignment rules on popular platforms;

Not necessarily.  You can trade this for performance if you're willing to
share the reference count with the color bits.  Normally, of course, I'd
take the 4 byte overhead.  Some applications (CAD tools, e.g.) do require
minimum sized objects since hundreds of millions of them may be active at
any one time...

> and, (c) the Lins "local" sweep too often ends
> up touching the whole world, since e.g. from any frame you can reach its
> function's module's global namespace, from which you can often reach sys,
> from whose sys.modules dict the entire system is reachable.

Yep, though, again, there are speed/memory usage tradeoffs you can apply
here (limit the search depth depending on the amount of memory needed,
etc.).  Now, you won't find me volunteering to tune the thing... :-)




More information about the Python-list mailing list