Skip to content

Commit e5ec0b3

Browse files
committed
gh-116946: remove unnecessary gc from _decimal types
1 parent 61818b6 commit e5ec0b3

2 files changed

Lines changed: 9 additions & 44 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Removed unnecessary gc support for :mod:`!_decimal` module. That provide
2+
1.07x-1.14x speedup on standard pyperformance benchmarks.

Modules/_decimal/_decimal.c

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,6 @@ static void
754754
signaldict_dealloc(PyObject *self)
755755
{
756756
PyTypeObject *tp = Py_TYPE(self);
757-
PyObject_GC_UnTrack(self);
758757
tp->tp_free(self);
759758
Py_DECREF(tp);
760759
}
@@ -842,7 +841,6 @@ static PyMethodDef signaldict_methods[] = {
842841

843842
static PyType_Slot signaldict_slots[] = {
844843
{Py_tp_dealloc, signaldict_dealloc},
845-
{Py_tp_traverse, _PyObject_VisitType},
846844
{Py_tp_repr, signaldict_repr},
847845
{Py_tp_hash, PyObject_HashNotImplemented},
848846
{Py_tp_getattro, PyObject_GenericGetAttr},
@@ -862,7 +860,7 @@ static PyType_Spec signaldict_spec = {
862860
.name = "decimal.SignalDictMixin",
863861
.basicsize = sizeof(PyDecSignalDictObject),
864862
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
865-
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
863+
Py_TPFLAGS_IMMUTABLETYPE),
866864
.slots = signaldict_slots,
867865
};
868866

@@ -1445,7 +1443,7 @@ context_new(PyTypeObject *type,
14451443

14461444
decimal_state *state = get_module_state_by_def(type);
14471445
if (type == state->PyDecContext_Type) {
1448-
self = PyObject_GC_New(PyDecContextObject, state->PyDecContext_Type);
1446+
self = PyObject_New(PyDecContextObject, state->PyDecContext_Type);
14491447
}
14501448
else {
14511449
self = (PyDecContextObject *)type->tp_alloc(type, 0);
@@ -1483,23 +1481,9 @@ context_new(PyTypeObject *type,
14831481
self->tstate = NULL;
14841482
self->modstate = state;
14851483

1486-
if (type == state->PyDecContext_Type) {
1487-
PyObject_GC_Track(self);
1488-
}
1489-
assert(PyObject_GC_IsTracked((PyObject *)self));
14901484
return (PyObject *)self;
14911485
}
14921486

1493-
static int
1494-
context_traverse(PyObject *op, visitproc visit, void *arg)
1495-
{
1496-
PyDecContextObject *self = _PyDecContextObject_CAST(op);
1497-
Py_VISIT(Py_TYPE(self));
1498-
Py_VISIT(self->traps);
1499-
Py_VISIT(self->flags);
1500-
return 0;
1501-
}
1502-
15031487
static int
15041488
context_clear(PyObject *op)
15051489
{
@@ -1527,7 +1511,6 @@ static void
15271511
context_dealloc(PyObject *self)
15281512
{
15291513
PyTypeObject *tp = Py_TYPE(self);
1530-
PyObject_GC_UnTrack(self);
15311514
(void)context_clear(self);
15321515
tp->tp_free(self);
15331516
Py_DECREF(tp);
@@ -2152,7 +2135,7 @@ _decimal_localcontext_impl(PyObject *module, PyObject *local, PyObject *prec,
21522135
}
21532136

21542137
PyDecContextManagerObject *self;
2155-
self = PyObject_GC_New(PyDecContextManagerObject,
2138+
self = PyObject_New(PyDecContextManagerObject,
21562139
state->PyDecContextManager_Type);
21572140
if (self == NULL) {
21582141
Py_DECREF(local_copy);
@@ -2161,21 +2144,10 @@ _decimal_localcontext_impl(PyObject *module, PyObject *local, PyObject *prec,
21612144

21622145
self->local = local_copy;
21632146
self->global = Py_NewRef(global);
2164-
PyObject_GC_Track(self);
21652147

21662148
return (PyObject *)self;
21672149
}
21682150

2169-
static int
2170-
ctxmanager_traverse(PyObject *op, visitproc visit, void *arg)
2171-
{
2172-
PyDecContextManagerObject *self = _PyDecContextManagerObject_CAST(op);
2173-
Py_VISIT(Py_TYPE(self));
2174-
Py_VISIT(self->local);
2175-
Py_VISIT(self->global);
2176-
return 0;
2177-
}
2178-
21792151
static int
21802152
ctxmanager_clear(PyObject *op)
21812153
{
@@ -2189,7 +2161,6 @@ static void
21892161
ctxmanager_dealloc(PyObject *self)
21902162
{
21912163
PyTypeObject *tp = Py_TYPE(self);
2192-
PyObject_GC_UnTrack(self);
21932164
(void)ctxmanager_clear(self);
21942165
tp->tp_free(self);
21952166
Py_DECREF(tp);
@@ -2233,7 +2204,6 @@ static PyMethodDef ctxmanager_methods[] = {
22332204
static PyType_Slot ctxmanager_slots[] = {
22342205
{Py_tp_dealloc, ctxmanager_dealloc},
22352206
{Py_tp_getattro, PyObject_GenericGetAttr},
2236-
{Py_tp_traverse, ctxmanager_traverse},
22372207
{Py_tp_clear, ctxmanager_clear},
22382208
{Py_tp_methods, ctxmanager_methods},
22392209
{0, NULL},
@@ -2242,7 +2212,7 @@ static PyType_Slot ctxmanager_slots[] = {
22422212
static PyType_Spec ctxmanager_spec = {
22432213
.name = "decimal.ContextManager",
22442214
.basicsize = sizeof(PyDecContextManagerObject),
2245-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2215+
.flags = (Py_TPFLAGS_DEFAULT |
22462216
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
22472217
.slots = ctxmanager_slots,
22482218
};
@@ -2258,7 +2228,7 @@ PyDecType_New(decimal_state *state, PyTypeObject *type)
22582228
PyDecObject *dec;
22592229

22602230
if (type == state->PyDec_Type) {
2261-
dec = PyObject_GC_New(PyDecObject, state->PyDec_Type);
2231+
dec = PyObject_New(PyDecObject, state->PyDec_Type);
22622232
}
22632233
else {
22642234
dec = (PyDecObject *)type->tp_alloc(type, 0);
@@ -2276,10 +2246,6 @@ PyDecType_New(decimal_state *state, PyTypeObject *type)
22762246
MPD(dec)->alloc = _Py_DEC_MINALLOC;
22772247
MPD(dec)->data = dec->data;
22782248

2279-
if (type == state->PyDec_Type) {
2280-
PyObject_GC_Track(dec);
2281-
}
2282-
assert(PyObject_GC_IsTracked((PyObject *)dec));
22832249
return (PyObject *)dec;
22842250
}
22852251
#define dec_alloc(st) PyDecType_New(st, (st)->PyDec_Type)
@@ -2288,7 +2254,6 @@ static void
22882254
dec_dealloc(PyObject *dec)
22892255
{
22902256
PyTypeObject *tp = Py_TYPE(dec);
2291-
PyObject_GC_UnTrack(dec);
22922257
mpd_del(MPD(dec));
22932258
tp->tp_free(dec);
22942259
Py_DECREF(tp);
@@ -6177,7 +6142,6 @@ static PyType_Slot dec_slots[] = {
61776142
{Py_tp_token, Py_TP_USE_SPEC},
61786143
{Py_tp_dealloc, dec_dealloc},
61796144
{Py_tp_getattro, PyObject_GenericGetAttr},
6180-
{Py_tp_traverse, _PyObject_VisitType},
61816145
{Py_tp_repr, dec_repr},
61826146
{Py_tp_hash, dec_hash},
61836147
{Py_tp_str, dec_str},
@@ -6210,7 +6174,7 @@ static PyType_Spec dec_spec = {
62106174
.name = "decimal.Decimal",
62116175
.basicsize = sizeof(PyDecObject),
62126176
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
6213-
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
6177+
Py_TPFLAGS_IMMUTABLETYPE),
62146178
.slots = dec_slots,
62156179
};
62166180

@@ -7616,7 +7580,6 @@ static PyMethodDef context_methods [] =
76167580
static PyType_Slot context_slots[] = {
76177581
{Py_tp_token, Py_TP_USE_SPEC},
76187582
{Py_tp_dealloc, context_dealloc},
7619-
{Py_tp_traverse, context_traverse},
76207583
{Py_tp_clear, context_clear},
76217584
{Py_tp_repr, context_repr},
76227585
{Py_tp_getattro, context_getattr},
@@ -7633,7 +7596,7 @@ static PyType_Spec context_spec = {
76337596
.name = "decimal.Context",
76347597
.basicsize = sizeof(PyDecContextObject),
76357598
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
7636-
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
7599+
Py_TPFLAGS_IMMUTABLETYPE),
76377600
.slots = context_slots,
76387601
};
76397602

0 commit comments

Comments
 (0)