Python and Boehm-Demers GC, I have code.

Tim Peters tim_one at email.msn.com
Fri Jul 16 22:18:42 EDT 1999


[Neil Schemenauer]
> I have added the Boehm-Demers garbage collector to Python and it
> works well!
> ...

That's good, but I'm not clear on what this means.  The patch didn't seem to
do more than replace malloc/calloc/realloc/free with the BDW versions; in
particular, Py_USE_GC_FREE was #define'd, and you ran tests without
significant cyclic structures, so it doesn't *appear* that anything more
happened here than that a different implementation of the malloc family got
plugged in.  Did the "collection" phase of BDW find any trash at all?

> I started with Sam Rushing's patch and modified it for Python
> 1.5.2c.  To my surprise, removing the reference counting
> (Py_INCREF, Py_DECREF) actually slowed down Python a lot (over
> two times for pystone).  I not exactly sure why this is.  Maybe
> someone can enlighten me.

My guess is that simple refcounting has excellent cache behavior in Python,
and GC doesn't.  Unlike as in almost any other language, the little loop

    for i in xrange(10000000):
        pass

creates a huge amount of heap trash at a furious pace (even ints "are boxed"
in Python).  RC can reuse the same little bit of storage over & over (the
trash is recycled as soon as it's created), while delayed GC chews up some
large amount of address space over & over instead.  You can test this by
timing that loop alone (with and without refcounting).

Note too that Python does no stack allocation of locals or frames or
anything else:  everything is heap-based, with the most speed-critical
allocations augmented by assorted internal fast free lists.  Disable the
refcounting, and the finalizers on the "intense" objects don't get called,
and then the fast free lists don't get used either.  So the allocation speed
optimizations Python implements get castrated too.

I've always said it would take a very sophisticated mark-&-sweep to beat
RC's performance for *Python* (it's not Lisp, or Smalltalk, or C++, or ...
under the covers); if true (& I haven't seen anything yet that changes my
mind <wink>), you're at the first step of very long road if you want to make
BDW the primary approach; but it would be great if BDW could be invoked
"just sometimes" to reclaim the cycles RC can't catch.

semi-encouraging-ly y'rs  - tim






More information about the Python-list mailing list