Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/internal/pycore_gc.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -168,7 +168,7 @@ PyAPI_FUNC(void) _PyGC_InitState(struct _gc_runtime_state *);
externvoid_PyFrame_ClearFreeList(PyThreadState*tstate);
externvoid_PyTuple_ClearFreeList(PyThreadState*tstate);
externvoid_PyFloat_ClearFreeList(PyThreadState*tstate);
externvoid_PyList_ClearFreeList(void);
externvoid_PyList_ClearFreeList(PyThreadState*tstate);
externvoid_PyDict_ClearFreeList(void);
externvoid_PyAsyncGen_ClearFreeLists(void);
externvoid_PyContext_ClearFreeList(void);
Expand Down
11 changes: 11 additions & 0 deletions Include/internal/pycore_interp.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,6 +84,16 @@ struct _Py_tuple_state{
#endif
};

/* Empty list reuse scheme to save calls to malloc and free */
#ifndefPyList_MAXFREELIST
# definePyList_MAXFREELIST 80
#endif

struct_Py_list_state{
PyListObject*free_list[PyList_MAXFREELIST];
intnumfree;
};

struct_Py_float_state{
/* Special free list
free_list is a singly-linked list of available PyFloatObjects,
Expand DownExpand Up@@ -192,6 +202,7 @@ struct _is{
PyLongObject*small_ints[_PY_NSMALLNEGINTS+_PY_NSMALLPOSINTS];
#endif
struct_Py_tuple_statetuple;
struct_Py_list_statelist;
struct_Py_float_statefloat_state;
struct_Py_frame_stateframe;

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_pylifecycle.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,7 +61,7 @@ extern PyStatus _PyGC_Init(PyThreadState *tstate);
externvoid_PyFrame_Fini(PyThreadState*tstate);
externvoid_PyDict_Fini(void);
externvoid_PyTuple_Fini(PyThreadState*tstate);
externvoid_PyList_Fini(void);
externvoid_PyList_Fini(PyThreadState*tstate);
externvoid_PySet_Fini(void);
externvoid_PyBytes_Fini(void);
externvoid_PyFloat_Fini(PyThreadState*tstate);
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
The tuple free lists, the empty tuple singleton, the float free list, the slice
cache, and the frame free list are no longer shared by all interpreters: each
interpreter now its has own free lists and caches.
The tuple free lists, the empty tuple singleton, the list free list, the float
free list, the slice cache, and the frame free list are no longer shared by all
interpreters: each interpreter now its has own free lists and caches.
2 changes: 1 addition & 1 deletion Modules/gcmodule.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1029,7 +1029,7 @@ clear_freelists(void)
_PyFrame_ClearFreeList(tstate);
_PyTuple_ClearFreeList(tstate);
_PyFloat_ClearFreeList(tstate);
_PyList_ClearFreeList();
_PyList_ClearFreeList(tstate);
_PyDict_ClearFreeList();
_PyAsyncGen_ClearFreeLists();
_PyContext_ClearFreeList();
Expand Down
60 changes: 29 additions & 31 deletions Objects/listobject.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -96,65 +96,59 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
return0;
}

/* Empty list reuse scheme to save calls to malloc and free */
#ifndefPyList_MAXFREELIST
# definePyList_MAXFREELIST 80
#endif

/* bpo-40521: list free lists are shared by all interpreters. */
#ifdefEXPERIMENTAL_ISOLATED_SUBINTERPRETERS
# undef PyList_MAXFREELIST
# definePyList_MAXFREELIST 0
#endif

staticPyListObject*free_list[PyList_MAXFREELIST];
staticintnumfree=0;

void
_PyList_ClearFreeList(void)
_PyList_ClearFreeList(PyThreadState*tstate)
{
while (numfree){
PyListObject*op=free_list[--numfree];
struct_Py_list_state*state=&tstate->interp->list;
while (state->numfree){
PyListObject*op=state->free_list[--state->numfree];
assert(PyList_CheckExact(op));
PyObject_GC_Del(op);
}
}

void
_PyList_Fini(void)
_PyList_Fini(PyThreadState*tstate)
{
_PyList_ClearFreeList();
_PyList_ClearFreeList(tstate);
}

/* Print summary info about the state of the optimized allocator */
void
_PyList_DebugMallocStats(FILE*out)
{
PyInterpreterState*interp=_PyInterpreterState_GET();
struct_Py_list_state*state=&interp->list;
_PyDebugAllocatorStats(out,
"free PyListObject",
numfree, sizeof(PyListObject));
state->numfree, sizeof(PyListObject));
}

PyObject*
PyList_New(Py_ssize_tsize)
{
PyListObject*op;

if (size<0){
PyErr_BadInternalCall();
returnNULL;
}
if (numfree){
numfree--;
op=free_list[numfree];

PyInterpreterState*interp=_PyInterpreterState_GET();
struct_Py_list_state*state=&interp->list;
PyListObject*op;
if (state->numfree){
state->numfree--;
op=state->free_list[state->numfree];
_Py_NewReference((PyObject*)op);
} else{
}
else{
op=PyObject_GC_New(PyListObject, &PyList_Type);
if (op==NULL)
if (op==NULL){
returnNULL;
}
}
if (size <= 0)
if (size <= 0){
op->ob_item=NULL;
}
else{
op->ob_item= (PyObject**) PyMem_Calloc(size, sizeof(PyObject*));
if (op->ob_item==NULL){
Expand DownExpand Up@@ -334,10 +328,14 @@ list_dealloc(PyListObject *op)
}
PyMem_FREE(op->ob_item);
}
if (numfree<PyList_MAXFREELIST&&PyList_CheckExact(op))
free_list[numfree++] =op;
else
PyInterpreterState*interp=_PyInterpreterState_GET();
struct_Py_list_state*state=&interp->list;
if (state->numfree<PyList_MAXFREELIST&&PyList_CheckExact(op)){
state->free_list[state->numfree++] =op;
}
else{
Py_TYPE(op)->tp_free((PyObject*)op);
}
Py_TRASHCAN_END
}

Expand Down
6 changes: 3 additions & 3 deletions Python/pylifecycle.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1251,8 +1251,8 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)
{
_PyFrame_Fini(tstate);
_PyTuple_Fini(tstate);
_PyList_Fini(tstate);
if (is_main_interp){
_PyList_Fini();
_PySet_Fini();
_PyBytes_Fini();
}
Expand DownExpand Up@@ -1296,6 +1296,8 @@ finalize_interp_clear(PyThreadState *tstate)
_PyGC_CollectNoFail();
}

_PyGC_Fini(tstate);

finalize_interp_types(tstate, is_main_interp);

if (is_main_interp){
Expand All@@ -1309,8 +1311,6 @@ finalize_interp_clear(PyThreadState *tstate)

_PyExc_Fini();
}

_PyGC_Fini(tstate);
}


Expand Down