[Python-Dev] C API for gc.enable() and gc.disable()

Alexandre Vassalotti alexandre at peadrop.com
Sun Jun 1 06:11:19 CEST 2008


Would anyone mind if I did add a public C API for gc.disable() and
gc.enable()? I would like to use it as an optimization for the pickle
module (I found out that I get a good 2x speedup just by disabling the
GC while loading large pickles). Of course, I could simply import the
gc module and call the functions there, but that seems overkill to me.
I included the patch below for review.

-- Alexandre



Index: Include/objimpl.h
===================================================================
--- Include/objimpl.h   (revision 63766)
+++ Include/objimpl.h   (working copy)
@@ -221,8 +221,10 @@
  * ==========================
  */

-/* C equivalent of gc.collect(). */
+/* C equivalent of gc.collect(), gc.enable() and gc.disable(). */
 PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
+PyAPI_FUNC(void) PyGC_Enable(void);
+PyAPI_FUNC(void) PyGC_Disable(void);

 /* Test if a type has a GC head */
 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Index: Modules/gcmodule.c
===================================================================
--- Modules/gcmodule.c  (revision 63766)
+++ Modules/gcmodule.c  (working copy)
@@ -1252,6 +1252,18 @@
        return n;
 }

+void
+PyGC_Disable(void)
+{
+    enabled = 0;
+}
+
+void
+PyGC_Enable(void)
+{
+    enabled = 1;
+}
+
 /* for debugging */
 void
 _PyGC_Dump(PyGC_Head *g)


More information about the Python-Dev mailing list