Skip to content

Commit 6bf41e5

Browse files
committed
Remove explicit empty tuple reuse in cpickle.
PyTuple_New(0) always returns the same empty tuple from its free list anyway, so we are not saving much here. Plus, the code where this was used is on uncommon run paths.
1 parent b13e6bc commit 6bf41e5

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

Modules/_pickle.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ typedef struct {
166166

167167
/* codecs.encode, used for saving bytes in older protocols */
168168
PyObject *codecs_encode;
169-
170-
/* As the name says, an empty tuple. */
171-
PyObject *empty_tuple;
172169
} PickleState;
173170

174171
/* Forward declaration of the _pickle module definition. */
@@ -205,7 +202,6 @@ _Pickle_ClearState(PickleState *st)
205202
Py_CLEAR(st->name_mapping_3to2);
206203
Py_CLEAR(st->import_mapping_3to2);
207204
Py_CLEAR(st->codecs_encode);
208-
Py_CLEAR(st->empty_tuple);
209205
}
210206

211207
/* Initialize the given pickle module state. */
@@ -321,10 +317,6 @@ _Pickle_InitState(PickleState *st)
321317
}
322318
Py_CLEAR(codecs);
323319

324-
st->empty_tuple = PyTuple_New(0);
325-
if (st->empty_tuple == NULL)
326-
goto error;
327-
328320
return 0;
329321

330322
error:
@@ -1137,8 +1129,9 @@ _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
11371129
return -1;
11381130

11391131
if (n == READ_WHOLE_LINE) {
1140-
PickleState *st = _Pickle_GetGlobalState();
1141-
data = PyObject_Call(self->readline, st->empty_tuple, NULL);
1132+
PyObject *empty_tuple = PyTuple_New(0);
1133+
data = PyObject_Call(self->readline, empty_tuple, NULL);
1134+
Py_DECREF(empty_tuple);
11421135
}
11431136
else {
11441137
PyObject *len = PyLong_FromSsize_t(n);
@@ -3774,8 +3767,10 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
37743767
/* Check for a __reduce__ method. */
37753768
reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
37763769
if (reduce_func != NULL) {
3777-
reduce_value = PyObject_Call(reduce_func, st->empty_tuple,
3770+
PyObject *empty_tuple = PyTuple_New(0);
3771+
reduce_value = PyObject_Call(reduce_func, empty_tuple,
37783772
NULL);
3773+
Py_DECREF(empty_tuple);
37793774
}
37803775
else {
37813776
PyErr_Format(st->PicklingError,
@@ -7412,7 +7407,6 @@ pickle_traverse(PyObject *m, visitproc visit, void *arg)
74127407
Py_VISIT(st->name_mapping_3to2);
74137408
Py_VISIT(st->import_mapping_3to2);
74147409
Py_VISIT(st->codecs_encode);
7415-
Py_VISIT(st->empty_tuple);
74167410
return 0;
74177411
}
74187412

0 commit comments

Comments
 (0)