8000 gh-126703: Add freelist for PyComplexObject's (gh-135233) · python/cpython@c646846 · GitHub
[go: up one dir, main page]

Skip to content

Commit c646846

Browse files
authored
gh-126703: Add freelist for PyComplexObject's (gh-135233)
1 parent eed827e commit c646846

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

Include/internal/pycore_freelist_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern "C" {
1616
# define Py_dicts_MAXFREELIST 80
1717
# define Py_dictkeys_MAXFREELIST 80
1818
# define Py_floats_MAXFREELIST 100
19+
# define Py_complexes_MAXFREELIST 100
1920
# define Py_ints_MAXFREELIST 100
2021
# define Py_slices_MAXFREELIST 1
2122
# define Py_ranges_MAXFREELIST 6
@@ -43,6 +44,7 @@ struct _Py_freelist {
4344

4445
struct _Py_freelists {
4546
struct _Py_freelist floats;
47+
struct _Py_freelist complexes;
4648
struct _Py_freelist ints;
4749
struct _Py_freelist tuples[PyTuple_MAXSAVESIZE];
4850
struct _Py_freelist lists;

Objects/complexobject.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/* Complex object implementation */
32

43
/* Borrows heavily from floatobject.c */
@@ -9,6 +8,7 @@
98
#include "pycore_call.h" // _PyObject_CallNoArgs()
109
#include "pycore_complexobject.h" // _PyComplex_FormatAdvancedWriter()
1110
#include "pycore_floatobject.h" // _Py_convert_int_to_double()
11+
#include "pycore_freelist.h" // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
1212
#include "pycore_long.h" // _PyLong_GetZero()
1313
#include "pycore_object.h" // _PyObject_Init()
1414
#include "pycore_pymath.h" // _Py_ADJUST_ERANGE2()
@@ -410,16 +410,32 @@ complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
410410
PyObject *
411411
PyComplex_FromCComplex(Py_complex cval)
412412
{
413-
/* Inline PyObject_New */
414-
PyComplexObject *op = PyObject_Malloc(sizeof(PyComplexObject));
413+
PyComplexObject *op = _Py_FREELIST_POP(PyComplexObject, complexes);
414+
415415
if (op == NULL) {
416-
return PyErr_NoMemory();
416+
/* Inline PyObject_New */
417+
op = PyObject_Malloc(sizeof(PyComplexObject));
418+
if (op == NULL) {
419+
return PyErr_NoMemory();
420+
}
421+
_PyObject_Init((PyObject*)op, &PyComplex_Type);
417422
}
418-
_PyObject_Init((PyObject*)op, &PyComplex_Type);
419423
op->cval = cval;
420424
return (PyObject *) op;
421425
}
422426

427+
static void
428+
complex_dealloc(PyObject *op)
429+
{
430+
assert(PyComplex_Check(op));
431+
if (PyComplex_CheckExact(op)) {
432+
_Py_FREELIST_FREE(complexes, op, PyObject_Free);
433+
}
434+
else {
435+
Py_TYPE(op)->tp_free(op);
436+
}
437+
}
438+
423439
static PyObject *
424440
complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
425441
{
@@ -1383,7 +1399,7 @@ PyTypeObject PyComplex_Type = {
13831399
"complex",
13841400
sizeof(PyComplexObject),
13851401
0,
1386-
0, /* tp_dealloc */
1402+
complex_dealloc, /* tp_dealloc */
13871403
0, /* tp_vectorcall_offset */
13881404
0, /* tp_getattr */
13891405
0, /* tp_setattr */

Objects/object.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization)
925925
// In the free-threaded build, freelists are per-PyThreadState and cleared in PyThreadState_Clear()
926926
// In the default build, freelists are per-interpreter and cleared in finalize_interp_types()
927927
clear_freelist(&freelists->floats, is_finalization, free_object);
928+
clear_freelist(&freelists->complexes, is_finalization, free_object);
928929
for (Py_ssize_t i = 0; i < PyTuple_MAXSAVESIZE; i++) {
929930
clear_freelist(&freelists->tuples[i], is_finalization, free_object);
930931
}

0 commit comments

Comments
 (0)
0