Skip to content
Closed
Prev Previous commit
Next Next commit
add PyCFunctionObject
  • Loading branch information
eendebakpt committed Jan 1, 2025
commit 5d13675cd8f2add3984ec7250e47120f93d4bd5b
4 changes: 4 additions & 0 deletions Include/internal/pycore_freelist_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ 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
Expand Down Expand Up @@ -46,6 +48,8 @@ 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;
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
2 changes: 2 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ _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