Python and Boehm-Demers GC, I have code.
Neil Schemenauer
nascheme at ucalgary.ca
Fri Jul 16 15:53:02 EDT 1999
I have added the Boehm-Demers garbage collector to Python and it
works well! With the default set of extension modules all tests
pass. Also I have done some benchmarks using pystone and
pybench. The GC version of Python is just as fast as the regular
version! I was quite amazed. Here are the pybench results:
PYBENCH 0.5
Benchmark: free3 (rounds=3, warp=10)
Tests: per run per op. diff *
------------------------------------------------------------------------
BuiltinFunctionCalls: 746.83 ms 2.93 us -11.90%
BuiltinMethodLookup: 1139.33 ms 1.09 us -1.07%
CreateInstances: 1006.83 ms 11.99 us -9.09%
DictCreation: 847.17 ms 2.82 us -21.66%
IfThenElse: 1357.67 ms 1.01 us -1.84%
ListSlicing: 1116.33 ms 127.58 us +38.30%
NestedForLoops: 970.00 ms 1.29 us -1.40%
NormalClassAttribute: 1126.50 ms 0.94 us -1.46%
NormalInstanceAttribute: 1086.33 ms 0.91 us -2.75%
PythonFunctionCalls: 1083.50 ms 3.28 us +3.63%
PythonMethodCalls: 837.83 ms 5.59 us +1.23%
Recursion: 981.50 ms 39.26 us +16.37%
SimpleDictManipulation: 934.67 ms 1.56 us -7.96%
SimpleFloatArithmetic: 1112.83 ms 1.01 us +4.95%
SimpleIntegerArithmetic: 1000.83 ms 0.76 us -2.48%
SimpleListManipulation: 1213.50 ms 2.25 us +36.74%
SmallLists: 1132.67 ms 2.22 us -15.34%
SmallTuples: 1198.50 ms 2.50 us -3.44%
SpecialClassAttribute: 1132.83 ms 0.94 us -1.51%
SpecialInstanceAttribute: 1299.67 ms 1.08 us -2.44%
TupleSlicing: 2027.17 ms 3.86 us -5.52%
------------------------------------------------------------------------
Average round time: 26870.00 ms -1.39%
*) measured against: py152 (rounds=10, warp=10)
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. I will try to do some more testing.
I have to fix up modules which use other mallocs (readline and
_tkinter for sure). Guido, will a patch be accepted which
changes Python code to use Py_Mem_* instead of the libc names
where approprate? I will have to go through the extension
modules and fix them by hand. Also, what about a patch that
makes using garbage collection a configure option. The changes
are quite minor. I don't know autoconf but maybe I can figure it
out.
With regards to finalization, __del__ methods are not called from
the garbage collector. If you create reference loops then too
bad for you. The garbage collector supports finalization so I
may fix this later.
A preliminary patch is attached. The patch assumes libgc.a and
gc.h are installed somewhere and that you have already run
configure. The Makefile diffs are probably okay only for a Linux
box. I must learn autoconfig. :(
Neil
diff -u -r Python-1.5.2c1/Include/mymalloc.h Python-gc/Include/mymalloc.h
--- Python-1.5.2c1/Include/mymalloc.h Fri Dec 4 11:48:10 1998
+++ Python-gc/Include/mymalloc.h Fri Jul 16 03:49:55 1999
@@ -87,6 +87,18 @@
#define _PyMem_EXTRA 0
#endif
+#ifdef Py_USE_GC
+#include <gc.h>
+#define malloc(n) GC_MALLOC(n)
+#define calloc(m,n) GC_MALLOC((m)*(n))
+#define realloc(o,n) GC_REALLOC(o,n)
+#ifdef Py_USE_GC_FREE
+ #define free(n) GC_FREE(n)
+#else
+ #define free(n)
+#endif /* Py_USE_GC_FREE */
+#endif /* Py_USE_GC */
+
#define PyMem_NEW(type, n) \
( (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)) )
#define PyMem_RESIZE(p, type, n) \
diff -u -r Python-1.5.2c1/Makefile Python-gc/Makefile
--- Python-1.5.2c1/Makefile Fri Jul 16 04:08:43 1999
+++ Python-gc/Makefile Fri Jul 16 04:08:37 1999
@@ -143,7 +143,7 @@
WITH=
# Compiler options passed to subordinate makes
-OPT= -g -O2
+OPT= -g -O2 -DPy_USE_GC
# Subdirectories where to run make recursively
SUBDIRS= Parser Objects Python Modules
diff -u -r Python-1.5.2c1/Modules/Makefile.pre Python-gc/Modules/Makefile.pre
--- Python-1.5.2c1/Modules/Makefile.pre Fri Jul 16 04:08:44 1999
+++ Python-gc/Modules/Makefile.pre Fri Jul 16 04:16:06 1999
@@ -28,7 +28,7 @@
SGI_ABI=
DEFS= -DHAVE_CONFIG_H
-LIBS= -lieee -ldl
+LIBS= -lieee -ldl -lgc
LIBM= -lm
LIBC=
diff -u -r Python-1.5.2c1/Objects/Makefile Python-gc/Objects/Makefile
--- Python-1.5.2c1/Objects/Makefile Fri Jul 16 04:08:43 1999
+++ Python-gc/Objects/Makefile Fri Jul 16 04:09:17 1999
@@ -13,7 +13,7 @@
RANLIB= ranlib
AR= ar
-DEFS= -DHAVE_CONFIG_H
+DEFS= -DHAVE_CONFIG_H -DPy_USE_GC_FREE
# === Other things that are customizable but not by configure ===
diff -u -r Python-1.5.2c1/Parser/Makefile Python-gc/Parser/Makefile
--- Python-1.5.2c1/Parser/Makefile Fri Jul 16 04:08:43 1999
+++ Python-gc/Parser/Makefile Fri Jul 16 04:08:52 1999
@@ -14,7 +14,7 @@
AR= ar
DEFS= -DHAVE_CONFIG_H
-LIBS= -lieee -ldl
+LIBS= -lieee -ldl -lgc
# === Other things that are customizable but not by configure ===
More information about the Python-list
mailing list