bpo-45522: Allow to disable freelists on build time (GH-29056)

Freelists for object structs can now be disabled. A new ``configure``
option ``--without-freelists`` can be used to disable all freelists
except empty tuple singleton. Internal Py*_MAXFREELIST macros can now
be defined as 0 without causing compiler warnings and segfaults.

Signed-off-by: Christian Heimes <christian@python.org>
This commit is contained in:
Christian Heimes 2021-10-21 16:12:20 +03:00 committed by GitHub
parent 5a14f71fe8
commit 9942f42a93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 218 additions and 35 deletions

View file

@ -9,9 +9,6 @@
#include "structmember.h" // PyMemberDef
#define CONTEXT_FREELIST_MAXLEN 255
#include "clinic/context.c.h"
/*[clinic input]
module _contextvars
@ -66,12 +63,14 @@ static int
contextvar_del(PyContextVar *var);
#if PyContext_MAXFREELIST > 0
static struct _Py_context_state *
get_context_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
return &interp->context;
}
#endif
PyObject *
@ -340,8 +339,9 @@ class _contextvars.Context "PyContext *" "&PyContext_Type"
static inline PyContext *
_context_alloc(void)
{
struct _Py_context_state *state = get_context_state();
PyContext *ctx;
#if PyContext_MAXFREELIST > 0
struct _Py_context_state *state = get_context_state();
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
assert(state->numfree != -1);
@ -353,7 +353,9 @@ _context_alloc(void)
ctx->ctx_weakreflist = NULL;
_Py_NewReference((PyObject *)ctx);
}
else {
else
#endif
{
ctx = PyObject_GC_New(PyContext, &PyContext_Type);
if (ctx == NULL) {
return NULL;
@ -469,17 +471,20 @@ context_tp_dealloc(PyContext *self)
}
(void)context_tp_clear(self);
#if PyContext_MAXFREELIST > 0
struct _Py_context_state *state = get_context_state();
#ifdef Py_DEBUG
// _context_alloc() must not be called after _PyContext_Fini()
assert(state->numfree != -1);
#endif
if (state->numfree < CONTEXT_FREELIST_MAXLEN) {
if (state->numfree < PyContext_MAXFREELIST) {
state->numfree++;
self->ctx_weakreflist = (PyObject *)state->freelist;
state->freelist = self;
}
else {
else
#endif
{
Py_TYPE(self)->tp_free(self);
}
}
@ -1289,6 +1294,7 @@ get_token_missing(void)
void
_PyContext_ClearFreeList(PyInterpreterState *interp)
{
#if PyContext_MAXFREELIST > 0
struct _Py_context_state *state = &interp->context;
for (; state->numfree; state->numfree--) {
PyContext *ctx = state->freelist;
@ -1296,6 +1302,7 @@ _PyContext_ClearFreeList(PyInterpreterState *interp)
ctx->ctx_weakreflist = NULL;
PyObject_GC_Del(ctx);
}
#endif
}
@ -1306,7 +1313,7 @@ _PyContext_Fini(PyInterpreterState *interp)
Py_CLEAR(_token_missing);
}
_PyContext_ClearFreeList(interp);
#ifdef Py_DEBUG
#if defined(Py_DEBUG) && PyContext_MAXFREELIST > 0
struct _Py_context_state *state = &interp->context;
state->numfree = -1;
#endif