Skip to content

Commit c3773e8

Browse files
committed
Add freelist for Decimal's
1 parent a9b6788 commit c3773e8

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

Include/internal/pycore_freelist_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern "C" {
1717
# define Py_dictkeys_MAXFREELIST 80
1818
# define Py_floats_MAXFREELIST 100
1919
# define Py_complexes_MAXFREELIST 100
20+
# define Py_decimals_MAXFREELIST 100
2021
# define Py_ints_MAXFREELIST 100
2122
# define Py_slices_MAXFREELIST 1
2223
# define Py_ranges_MAXFREELIST 6
@@ -47,6 +48,7 @@ struct _Py_freelists {
4748
struct _Py_freelist floats;
4849
struct _Py_freelist complexes;
4950
struct _Py_freelist ints;
51+
struct _Py_freelist decimals;
5052
struct _Py_freelist tuples[PyTuple_MAXSAVESIZE];
5153
struct _Py_freelist lists;
5254
struct _Py_freelist list_iters;

Modules/_decimal/_decimal.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#endif
3131

3232
#include <Python.h>
33+
#include "pycore_freelist.h" // _Py_FREELIST_POP()
3334
#include "pycore_object.h" // _PyObject_VisitType()
3435
#include "pycore_pystate.h" // _PyThreadState_GET()
3536
#include "pycore_typeobject.h"
@@ -2196,7 +2197,10 @@ PyDecType_New(decimal_state *state, PyTypeObject *type)
21962197
PyDecObject *dec;
21972198

21982199
if (type == state->PyDec_Type) {
2199-
dec = PyObject_GC_New(PyDecObject, state->PyDec_Type);
2200+
dec = _Py_FREELIST_POP(PyDecObject, decimals);
2201+
if (dec == NULL) {
2202+
dec = PyObject_GC_New(PyDecObject, state->PyDec_Type);
2203+
}
22002204
}
22012205
else {
22022206
dec = (PyDecObject *)type->tp_alloc(type, 0);
@@ -2226,10 +2230,16 @@ static void
22262230
dec_dealloc(PyObject *dec)
22272231
{
22282232
PyTypeObject *tp = Py_TYPE(dec);
2233+
decimal_state *state = get_module_state_by_def(tp);
2234+
22292235
PyObject_GC_UnTrack(dec);
22302236
mpd_del(MPD(dec));
2231-
tp->tp_free(dec);
2232-
Py_DECREF(tp);
2237+
if (!PyDec_CheckExact(state, dec)
2238+
|| !_Py_FREELIST_PUSH(decimals, dec, Py_decimals_MAXFREELIST))
2239+
{
2240+
tp->tp_free(dec);
2241+
Py_DECREF(tp);
2242+
}
22332243
}
22342244

22352245

Objects/object.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization)
951951
clear_freelist(&freelists->pycfunctionobject, is_finalization, PyObject_GC_Del);
952952
clear_freelist(&freelists->pycmethodobject, is_finalization, PyObject_GC_Del);
953953
clear_freelist(&freelists->pymethodobjects, is_finalization, free_object);
954+
clear_freelist(&freelists->decimals, is_finalization, free_object);
954955
}
955956

956957
/*

0 commit comments

Comments
 (0)