Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-126703: Add freelists for iterators and range, method and builtin_function_or_method objects #128368

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions Include/internal/pycore_freelist_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ extern "C" {
# define Py_floats_MAXFREELIST 100
# define Py_ints_MAXFREELIST 100
# define Py_slices_MAXFREELIST 1
# define Py_pycfunctionobject_MAXFREELIST 40
# define Py_pycmethodobject_MAXFREELIST 40
# define Py_ranges_MAXFREELIST 10
# define Py_range_iters_MAXFREELIST 10
# define Py_shared_iters_MAXFREELIST 24
# define Py_class_method_MAXFREELIST 10
# define Py_contexts_MAXFREELIST 255
# define Py_async_gens_MAXFREELIST 80
# define Py_async_gen_asends_MAXFREELIST 80
Expand All @@ -42,6 +48,12 @@ struct _Py_freelists {
struct _Py_freelist dicts;
struct _Py_freelist dictkeys;
struct _Py_freelist slices;
struct _Py_freelist pycfunctionobject;
struct _Py_freelist pycmethodobject;
struct _Py_freelist ranges;
struct _Py_freelist range_iters;
struct _Py_freelist shared_iters;
struct _Py_freelist class_method;
struct _Py_freelist contexts;
struct _Py_freelist async_gens;
struct _Py_freelist async_gen_asends;
Expand Down
11 changes: 8 additions & 3 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_VectorcallTstate()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_freelist.h"
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
Expand Down Expand Up @@ -112,9 +113,13 @@ PyMethod_New(PyObject *func, PyObject *self)
PyErr_BadInternalCall();
return NULL;
}
PyMethodObject *im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);

PyMethodObject *im = _Py_FREELIST_POP(PyMethodObject, class_method);
if (im == NULL) {
return NULL;
im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
if (im == NULL) {
return NULL;
}
}
im->im_weakreflist = NULL;
im->im_func = Py_NewRef(func);
Expand Down Expand Up @@ -245,7 +250,7 @@ method_dealloc(PyObject *self)
PyObject_ClearWeakRefs((PyObject *)im);
Py_DECREF(im->im_func);
Py_XDECREF(im->im_self);
PyObject_GC_Del(im);
_Py_FREELIST_FREE(class_method, (PyObject *)im, PyObject_GC_Del);
}

static PyObject *
Expand Down
17 changes: 13 additions & 4 deletions Objects/iterobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "pycore_abstract.h" // _PyObject_HasLen()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_freelist.h" // _Py_FREELIST_PUSH(), _Py_FREELIST_POP()
#include "pycore_object.h" // _PyObject_GC_TRACK()

typedef struct {
Expand All @@ -21,9 +22,16 @@ PySeqIter_New(PyObject *seq)
PyErr_BadInternalCall();
return NULL;
}
it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
if (it == NULL)
return NULL;

it = _Py_FREELIST_POP(seqiterobject, shared_iters);
if (it == NULL) {
it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
if (it == NULL)
return NULL;
}
else {
Py_SET_TYPE(it, &PySeqIter_Type);
}
it->it_index = 0;
it->it_seq = Py_NewRef(seq);
_PyObject_GC_TRACK(it);
Expand All @@ -35,7 +43,8 @@ iter_dealloc(seqiterobject *it)
{
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
assert(sizeof(seqiterobject)==sizeof(_PyListIterObject));
_Py_FREELIST_FREE(shared_iters, (PyObject *)it, PyObject_GC_Del);
}

static int
Expand Down
16 changes: 10 additions & 6 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3906,15 +3906,19 @@ PyTypeObject PyListIter_Type = {
static PyObject *
list_iter(PyObject *seq)
{
_PyListIterObject *it;

if (!PyList_Check(seq)) {
PyErr_BadInternalCall();
return NULL;
}
it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
if (it == NULL)
return NULL;
_PyListIterObject *it = _Py_FREELIST_POP(_PyListIterObject, shared_iters);
if (it == NULL) {
it = PyObject_GC_New(_PyListIterObject, &PyListIter_Type);
if (it == NULL) {
return NULL;
}
} else {
Py_SET_TYPE(it, &PyListIter_Type);
}
it->it_index = 0;
it->it_seq = (PyListObject *)Py_NewRef(seq);
_PyObject_GC_TRACK(it);
Expand All @@ -3927,7 +3931,7 @@ listiter_dealloc(PyObject *self)
_PyListIterObject *it = (_PyListIterObject *)self;
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
_Py_FREELIST_FREE(shared_iters, it, PyObject_GC_Del);
}

static int
Expand Down
26 changes: 19 additions & 7 deletions Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_call.h" // _Py_CheckFunctionResult()
#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
#include "pycore_freelist.h"
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
Expand Down Expand Up @@ -85,10 +86,13 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c
"flag but no class");
return NULL;
}
PyCMethodObject *om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
if (om == NULL) {
return NULL;
}
PyCMethodObject *om = _Py_FREELIST_POP(PyCMethodObject, pycmethodobject);
if (om == NULL) {
om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
if (om == NULL) {
return NULL;
}
}
om->mm_class = (PyTypeObject*)Py_NewRef(cls);
op = (PyCFunctionObject *)om;
} else {
Expand All @@ -98,9 +102,12 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c
"but no METH_METHOD flag");
return NULL;
}
op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
op = _Py_FREELIST_POP(PyCFunctionObject, pycfunctionobject);
if (op == NULL) {
return NULL;
op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
if (op == NULL) {
return NULL;
}
}
}

Expand Down Expand Up @@ -171,7 +178,12 @@ meth_dealloc(PyObject *self)
Py_XDECREF(PyCFunction_GET_CLASS(m));
Py_XDECREF(m->m_self);
Py_XDECREF(m->m_module);
PyObject_GC_Del(m);
if (m->m_ml->ml_flags & METH_METHOD) {
_Py_FREELIST_FREE(pycmethodobject, m, PyObject_GC_Del);
}
else {
_Py_FREELIST_FREE(pycfunctionobject, m, PyObject_GC_Del);
}
Py_TRASHCAN_END;
}

Expand Down
6 changes: 6 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,12 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization)
}
clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free);
clear_freelist(&freelists->ints, is_finalization, free_object);
clear_freelist(&freelists->shared_iters, is_finalization, free_object);
clear_freelist(&freelists->pycfunctionobject, is_finalization, PyObject_GC_Del);
clear_freelist(&freelists->pycmethodobject, is_finalization, PyObject_GC_Del);
clear_freelist(&freelists->ranges, is_finalization, free_object);
clear_freelist(&freelists->range_iters, is_finalization, free_object);
clear_freelist(&freelists->class_method, is_finalization, free_object);
}

/*
Expand Down
33 changes: 24 additions & 9 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_freelist.h" // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_range.h"
Expand Down Expand Up @@ -51,16 +52,18 @@ static rangeobject *
make_range_object(PyTypeObject *type, PyObject *start,
PyObject *stop, PyObject *step)
{
rangeobject *obj = NULL;
PyObject *length;
length = compute_range_length(start, stop, step);
if (length == NULL) {
return NULL;
}
obj = PyObject_New(rangeobject, type);
rangeobject *obj = _Py_FREELIST_POP(rangeobject, ranges);
if (obj == NULL) {
Py_DECREF(length);
return NULL;
obj = PyObject_New(rangeobject, type);
if (obj == NULL) {
Py_DECREF(length);
return NULL;
}
}
obj->start = start;
obj->stop = stop;
Expand Down Expand Up @@ -170,7 +173,7 @@ range_dealloc(rangeobject *r)
Py_DECREF(r->stop);
Py_DECREF(r->step);
Py_DECREF(r->length);
PyObject_Free(r);
_Py_FREELIST_FREE(ranges, r, PyObject_Free);
}

static unsigned long
Expand Down Expand Up @@ -880,6 +883,12 @@ rangeiter_setstate(_PyRangeIterObject *r, PyObject *state)
Py_RETURN_NONE;
}

static void
rangeiter_dealloc(_PyRangeIterObject *r)
{
_Py_FREELIST_FREE(range_iters, r, PyObject_Free);
}

PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");

Expand All @@ -899,7 +908,7 @@ PyTypeObject PyRangeIter_Type = {
sizeof(_PyRangeIterObject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)PyObject_Free, /* tp_dealloc */
(destructor)rangeiter_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
Expand Down Expand Up @@ -960,9 +969,15 @@ get_len_of_range(long lo, long hi, long step)
static PyObject *
fast_range_iter(long start, long stop, long step, long len)
{
_PyRangeIterObject *it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
if (it == NULL)
return NULL;
_PyRangeIterObject *it = _Py_FREELIST_POP(_PyRangeIterObject, range_iters);
if (it == NULL) {
it = PyObject_New(_PyRangeIterObject, &PyRangeIter_Type);
if (it == NULL)
return NULL;
}
else {
Py_SET_TYPE(it, &PyRangeIter_Type);
}
it->start = start;
it->step = step;
it->len = len;
Expand Down
15 changes: 11 additions & 4 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,8 @@ tupleiter_dealloc(_PyTupleIterObject *it)
{
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
assert(sizeof(_PyTupleIterObject)==sizeof(_PyListIterObject));
_Py_FREELIST_FREE(shared_iters, it, PyObject_GC_Del);
}

static int
Expand Down Expand Up @@ -1122,9 +1123,15 @@ tuple_iter(PyObject *seq)
PyErr_BadInternalCall();
return NULL;
}
it = PyObject_GC_New(_PyTupleIterObject, &PyTupleIter_Type);
if (it == NULL)
return NULL;
it = _Py_FREELIST_POP(_PyTupleIterObject, shared_iters);

if (it == NULL) {
it = PyObject_GC_New(_PyTupleIterObject, &PyTupleIter_Type);
if (it == NULL)
return NULL;
} else {
Py_SET_TYPE(it, &PyTupleIter_Type);
}
it->it_index = 0;
it->it_seq = (PyTupleObject *)Py_NewRef(seq);
_PyObject_GC_TRACK(it);
Expand Down
Loading