Skip to content

Commit f031f94

Browse files
committed
gh-126703: add freelist for PyComplexObject's
1 parent 4615164 commit f031f94

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

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 */

0 commit comments

Comments
 (0)