summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-02-04 18:00:12 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-02-04 18:00:12 (GMT)
commit422051a3675271e179995ad4a0f056ff94395d55 (patch)
tree3316e9503901082cc4beefbf169d8191c9838190 /Objects
parenta26cf9b7609fc1c08fd1a69ddf5e44dc98a70dce (diff)
downloadcpython-422051a3675271e179995ad4a0f056ff94395d55.zip
cpython-422051a3675271e179995ad4a0f056ff94395d55.tar.gz
cpython-422051a3675271e179995ad4a0f056ff94395d55.tar.bz2
Patch #1953
I implemented the function sys._compact_freelists() and C API functions PyInt_/PyFloat_CompactFreeList() to compact the pre-allocated blocks of ints and floats. They allow the user to reduce the memory usage of a Python process that deals with lots of numbers. The patch also renames sys._cleartypecache to sys._clear_type_cache
Diffstat (limited to 'Objects')
-rw-r--r--Objects/floatobject.c30
-rw-r--r--Objects/intobject.c52
2 files changed, 57 insertions, 25 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 5e9371d..19149af 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1609,17 +1609,15 @@ _PyFloat_Init(void)
}
void
-PyFloat_Fini(void)
+PyFloat_CompactFreeList(size_t *pbc, size_t *pbf, size_t *bsum)
{
PyFloatObject *p;
PyFloatBlock *list, *next;
unsigned i;
- int bc, bf; /* block count, number of freed blocks */
- int frem, fsum; /* remaining unfreed floats per block, total */
+ size_t bc = 0, bf = 0; /* block count, number of freed blocks */
+ size_t fsum = 0; /* total unfreed ints */
+ int frem; /* remaining unfreed ints per block */
- bc = 0;
- bf = 0;
- fsum = 0;
list = block_list;
block_list = NULL;
free_list = NULL;
@@ -1654,6 +1652,22 @@ PyFloat_Fini(void)
fsum += frem;
list = next;
}
+ *pbc = bc;
+ *pbf = bf;
+ *bsum = fsum;
+}
+
+void
+PyFloat_Fini(void)
+{
+ PyFloatObject *p;
+ PyFloatBlock *list;
+ unsigned i;
+ size_t bc, bf; /* block count, number of freed blocks */
+ size_t fsum; /* total unfreed floats per block */
+
+ PyFloat_CompactFreeList(&bc, &bf, &fsum);
+
if (!Py_VerboseFlag)
return;
fprintf(stderr, "# cleanup floats");
@@ -1662,7 +1676,9 @@ PyFloat_Fini(void)
}
else {
fprintf(stderr,
- ": %d unfreed float%s in %d out of %d block%s\n",
+ ": %" PY_FORMAT_SIZE_T "d unfreed floats%s in %"
+ PY_FORMAT_SIZE_T "d out of %"
+ PY_FORMAT_SIZE_T "d block%s\n",
fsum, fsum == 1 ? "" : "s",
bc - bf, bc, bc == 1 ? "" : "s");
}
diff --git a/Objects/intobject.c b/Objects/intobject.c
index edb8e4f..7c2a6fb 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -1202,28 +1202,15 @@ _PyInt_Init(void)
}
void
-PyInt_Fini(void)
+PyInt_CompactFreeList(size_t *pbc, size_t *pbf, size_t *bsum)
{
PyIntObject *p;
PyIntBlock *list, *next;
- int i;
unsigned int ctr;
- int bc, bf; /* block count, number of freed blocks */
- int irem, isum; /* remaining unfreed ints per block, total */
+ size_t bc = 0, bf = 0; /* block count, number of freed blocks */
+ size_t isum = 0; /* total unfreed ints */
+ int irem; /* remaining unfreed ints per block */
-#if NSMALLNEGINTS + NSMALLPOSINTS > 0
- PyIntObject **q;
-
- i = NSMALLNEGINTS + NSMALLPOSINTS;
- q = small_ints;
- while (--i >= 0) {
- Py_XDECREF(*q);
- *q++ = NULL;
- }
-#endif
- bc = 0;
- bf = 0;
- isum = 0;
list = block_list;
block_list = NULL;
free_list = NULL;
@@ -1268,6 +1255,33 @@ PyInt_Fini(void)
isum += irem;
list = next;
}
+
+ *pbc = bc;
+ *pbf = bf;
+ *bsum = isum;
+}
+
+void
+PyInt_Fini(void)
+{
+ PyIntObject *p;
+ PyIntBlock *list;
+ unsigned int ctr;
+ size_t bc, bf; /* block count, number of freed blocks */
+ size_t isum; /* total unfreed ints per block */
+
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0
+ int i;
+ PyIntObject **q;
+
+ i = NSMALLNEGINTS + NSMALLPOSINTS;
+ q = small_ints;
+ while (--i >= 0) {
+ Py_XDECREF(*q);
+ *q++ = NULL;
+ }
+#endif
+ PyInt_CompactFreeList(&bc, &bf, &isum);
if (!Py_VerboseFlag)
return;
fprintf(stderr, "# cleanup ints");
@@ -1276,7 +1290,9 @@ PyInt_Fini(void)
}
else {
fprintf(stderr,
- ": %d unfreed int%s in %d out of %d block%s\n",
+ ": %" PY_FORMAT_SIZE_T "d unfreed ints%s in %"
+ PY_FORMAT_SIZE_T "d out of %"
+ PY_FORMAT_SIZE_T "d block%s\n",
isum, isum == 1 ? "" : "s",
bc - bf, bc, bc == 1 ? "" : "s");
}