8000 add PyCFunctionObject · python/cpython@5d13675 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d13675

Browse files
committed
add PyCFunctionObject
1 parent fce4651 commit 5d13675

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

Include/internal/pycore_freelist_state.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern "C" {
1616
# define Py_floats_MAXFREELIST 100
1717
# define Py_ints_MAXFREELIST 100
1818
# define Py_slices_MAXFREELIST 1
19+
# define Py_pycfunctionobject_MAXFREELIST 40
20+
# define Py_pycmethodobject_MAXFREELIST 40
1921
# define Py_ranges_MAXFREELIST 10
2022
# define Py_range_iters_MAXFREELIST 10
2123
# define Py_shared_iters_MAXFREELIST 24
@@ -46,6 +48,8 @@ struct _Py_freelists {
4648
struct _Py_freelist dicts;
4749
struct _Py_freelist dictkeys;
4850
struct _Py_freelist slices;
51+
struct _Py_freelist pycfunctionobject;
52+
struct _Py_freelist pycmethodobject;
4953
struct _Py_freelist ranges;
5054
struct _Py_freelist range_iters;
5155
struct _Py_freelist shared_iters;

Objects/methodobject.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Python.h"
55
#include "pycore_call.h" // _Py_CheckFunctionResult()
66
#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
7+
#include "pycore_freelist.h"
78
#include "pycore_object.h"
89 8000
#include "pycore_pyerrors.h"
910
#include "pycore_pystate.h" // _PyThreadState_GET()
@@ -85,10 +86,13 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c
8586
"flag but no class");
8687
return NULL;
8788
}
88-
PyCMethodObject *om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
89-
if (om == NULL) {
90-
return NULL;
91-
}
89+
PyCMethodObject *om = _Py_FREELIST_POP(PyCMethodObject, pycmethodobject);
90+
if (om == NULL) {
91+
om = PyObject_GC_New(PyCMethodObject, &PyCMethod_Type);
92+
if (om == NULL) {
93+
return NULL;
94+
}
95+
}
9296
om->mm_class = (PyTypeObject*)Py_NewRef(cls);
9397
op = (PyCFunctionObject *)om;
9498
} else {
@@ -98,9 +102,12 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c
98102
"but no METH_METHOD flag");
99103
return NULL;
100104
}
101-
op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
105+
op = _Py_FREELIST_POP(PyCFunctionObject, pycfunctionobject);
102106
if (op == NULL) {
103-
return NULL;
107+
op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
108+
if (op == NULL) {
109+
return NULL;
110+
}
104111
}
105112
}
106113

@@ -171,7 +178,12 @@ meth_dealloc(PyObject *self)
171178
Py_XDECREF(PyCFunction_GET_CLASS(m));
172179
Py_XDECREF(m->m_self);
173180
Py_XDECREF(m->m_module);
174-
PyObject_GC_Del(m);
181+
if (m->m_ml->ml_flags & METH_METHOD) {
182+
_Py_FREELIST_FREE(pycmethodobject, m, PyObject_GC_Del);
183+
}
184+
else {
185+
_Py_FREELIST_FREE(pycfunctionobject, m, PyObject_GC_Del);
186+
}
175187
Py_TRASHCAN_END;
176188
}
177189

Objects/object.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization)
938938
clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free);
939939
clear_freelist(&freelists->ints, is_finalization, free_object);
940940
clear_freelist(&freelists->shared_iters, is_finalization, free_object);
941+
clear_freelist(&freelists->pycfunctionobject, is_finalization, PyObject_GC_Del);
942+
clear_freelist(&freelists->pycmethodobject, is_finalization, PyObject_GC_Del);
941943
clear_freelist(&freelists->ranges, is_finalization, free_object);
942944
clear_freelist(&freelists->range_iters, is_finalization, free_object);
943945
clear_freelist(&freelists->class_method, is_finalization, free_object);

0 commit comments

Comments
 (0)
0