Python's garbage collection was Re: Python reliability

Tom Anderson twic at urchin.earth.li
Wed Oct 12 20:30:23 EDT 2005


On Tue, 11 Oct 2005, Alex Martelli wrote:

> Tom Anderson <twic at urchin.earth.li> wrote:
>   ...
>> Has anyone looked into using a real GC for python? I realise it would be a
>
> If you mean mark-and-sweep, with generational twists,

Yes, more or less.

> that's what gc uses for cyclic garbage.

Do you mean what python uses for cyclic garbage? If so, i hadn't realised 
that. There are algorithms for extending refcounting to cyclic structures 
(i forget the details, but you sort of go round and experimentally 
decrement an object's count and see it ends up with a negative count or 
something), so i assumed python used one of those. Mind you, those are 
probably more complex than mark-and-sweep!

>> lot more complexity in the interpreter itself, but it would be faster, 
>> more reliable, and would reduce the complexity of extensions.
>
> ???  It adds no complexity (it's already there), it's slower,

Ah. That would be why all those java, .net, LISP, smalltalk and assorted 
other VMs out there, with decades of development, hojillions of dollars 
and the serried ranks of some of the greatest figures in computer science 
behind them all use reference counting rather than garbage collection, 
then.

No, wait ...

> it is, if anything, LESS reliable than reference counting (which is way 
> simpler!),

Reliability is a red herring - in the absence of ill-behaved native 
extensions, and with correct implementations, both refcounting and GC are 
perfectly reliable. And you can rely on the implementation being correct, 
since any incorrectness will be detected very quickly!

> and (if generalized to deal with ALL garbage) it might make it almost 
> impossible to write some kinds of extensions (ones which need to 
> interface existing C libraries that don't cooperate with whatever GC 
> collection you choose).

Lucky those existing C libraries were written to use python's refcounting!

Oh, you have to write a wrapper round the library to interface with the 
automatic memory management? Well, as it happens, the stuff you need to do 
is more or less identical for refcounting and GC - the extension has to 
tell the VM which of the VM's objects it holds references to, so that the 
VM knows that they aren't garbage.

> Are we talking about the same thing?!

Doesn't look like it, does it?

>> So python doesn't use the old SmallTalk 80 SmallInteger hack, or similar?
>> Fair enough - the performance gain is nice, but the extra complexity would
>> be a huge pain, i imagine.
>
> CPython currently is implemented on a strict "minimize all tricks" 
> strategy.

A very, very sound principle. If you have the aforementioned decades, 
hojillions and serried ranks, an all-tricks-turned-up-to-eleven strategy 
can be made to work. If you're a relatively small non-profit outfit like 
the python dev team, minimising tricks buys you reliability and agility, 
which is, really, what we all want.

tom

-- 
That's no moon!



More information about the Python-list mailing list