mirror of
https://github.com/python/cpython.git
synced 2025-07-31 07:04:42 +00:00
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
This commit is contained in:
parent
9521f08b98
commit
b4ee4a16f4
2 changed files with 54 additions and 0 deletions
|
@ -63,6 +63,22 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Debug statistic to compare allocations with reuse through the free list */
|
||||
#undef SHOW_ALLOC_COUNT
|
||||
#ifdef SHOW_ALLOC_COUNT
|
||||
static size_t count_alloc = 0;
|
||||
static size_t count_reuse = 0;
|
||||
|
||||
static void
|
||||
show_alloc(void)
|
||||
{
|
||||
fprintf(stderr, "List allocations: %zd\n", count_alloc);
|
||||
fprintf(stderr, "List reuse through freelist: %zd\n", count_reuse);
|
||||
fprintf(stderr, "%.2f%% reuse rate\n\n",
|
||||
(100.0*count_reuse/(count_alloc+count_reuse)));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Empty list reuse scheme to save calls to malloc and free */
|
||||
#ifndef PyList_MAXFREELIST
|
||||
#define PyList_MAXFREELIST 80
|
||||
|
@ -88,6 +104,13 @@ PyList_New(Py_ssize_t size)
|
|||
{
|
||||
PyListObject *op;
|
||||
size_t nbytes;
|
||||
#ifdef SHOW_ALLOC_COUNT
|
||||
static int initialized = 0;
|
||||
if (!initialized) {
|
||||
Py_AtExit(show_alloc);
|
||||
initialized = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (size < 0) {
|
||||
PyErr_BadInternalCall();
|
||||
|
@ -101,10 +124,16 @@ PyList_New(Py_ssize_t size)
|
|||
numfree--;
|
||||
op = free_list[numfree];
|
||||
_Py_NewReference((PyObject *)op);
|
||||
#ifdef SHOW_ALLOC_COUNT
|
||||
count_reuse++;
|
||||
#endif
|
||||
} else {
|
||||
op = PyObject_GC_New(PyListObject, &PyList_Type);
|
||||
if (op == NULL)
|
||||
return NULL;
|
||||
#ifdef SHOW_ALLOC_COUNT
|
||||
count_alloc++;
|
||||
#endif
|
||||
}
|
||||
if (size <= 0)
|
||||
op->ob_item = NULL;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue