Skip to content

Add _PyTuple_New_Nonzeroed #96446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Include/internal/pycore_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ struct _Py_tuple_state {
extern PyObject *_PyTuple_FromArray(PyObject *const *, Py_ssize_t);
extern PyObject *_PyTuple_FromArraySteal(PyObject *const *, Py_ssize_t);

/* A faster, but unsafe, version of PyTuple_New for use in performance-
* critical paths.
*
* Zeroing the contents of a tuple is surprisingly expensive, so for
* cases where the contents of the tuple will immediately be overwritten,
* time can be saved by calling this non-zeroing version.
*
* This tuple is not safe to be seen by any other code since it will
* have garbage where PyObject pointers are expected. If there is any
* chance that the initialization code will raise an exception or do
* a GC collection, it is not safe to use this function.
*/
PyAPI_FUNC(PyObject *) _PyTuple_New_Nonzeroed(Py_ssize_t size);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
Py_DECREF(rval);
return NULL;
}
tpl = PyTuple_New(2);
tpl = _PyTuple_New_Nonzeroed(2);
if (tpl == NULL) {
Py_DECREF(pyidx);
Py_DECREF(rval);
Expand Down
4 changes: 2 additions & 2 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4281,7 +4281,7 @@ dictiter_iternextitem(dictiterobject *di)
}
}
else {
result = PyTuple_New(2);
result = _PyTuple_New_Nonzeroed(2);
if (result == NULL)
return NULL;
PyTuple_SET_ITEM(result, 0, key); /* steals reference */
Expand Down Expand Up @@ -4415,7 +4415,7 @@ dictreviter_iternext(dictiterobject *di)
}
}
else {
result = PyTuple_New(2);
result = _PyTuple_New_Nonzeroed(2);
if (result == NULL) {
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ enum_next_long(enumobject *en, PyObject* next_item)
}
return result;
}
result = PyTuple_New(2);
result = _PyTuple_New_Nonzeroed(2);
if (result == NULL) {
Py_DECREF(next_index);
Py_DECREF(next_item);
Expand Down Expand Up @@ -257,7 +257,7 @@ enum_next(enumobject *en)
}
return result;
}
result = PyTuple_New(2);
result = _PyTuple_New_Nonzeroed(2);
if (result == NULL) {
Py_DECREF(next_index);
Py_DECREF(next_item);
Expand Down
4 changes: 2 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4249,7 +4249,7 @@ long_divmod(PyObject *a, PyObject *b)
if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
return NULL;
}
z = PyTuple_New(2);
z = _PyTuple_New_Nonzeroed(2);
if (z != NULL) {
PyTuple_SET_ITEM(z, 0, (PyObject *) div);
PyTuple_SET_ITEM(z, 1, (PyObject *) mod);
Expand Down Expand Up @@ -5544,7 +5544,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
goto error;
}

result = PyTuple_New(2);
result = _PyTuple_New_Nonzeroed(2);
if (result == NULL)
goto error;

Expand Down
2 changes: 1 addition & 1 deletion Objects/odictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,7 @@ odictiter_iternext(odictiterobject *di)
}
}
else {
result = PyTuple_New(2);
result = _PyTuple_New_Nonzeroed(2);
if (result == NULL) {
Py_DECREF(key);
Py_DECREF(value);
Expand Down
4 changes: 2 additions & 2 deletions Objects/stringlib/partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ STRINGLIB(partition)(PyObject* str_obj,
return NULL;
}

out = PyTuple_New(3);
out = _PyTuple_New_Nonzeroed(3);
if (!out)
return NULL;

Expand Down Expand Up @@ -80,7 +80,7 @@ STRINGLIB(rpartition)(PyObject* str_obj,
return NULL;
}

out = PyTuple_New(3);
out = _PyTuple_New_Nonzeroed(3);
if (!out)
return NULL;

Expand Down
15 changes: 15 additions & 0 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ PyTuple_New(Py_ssize_t size)
return (PyObject *) op;
}

PyObject *
_PyTuple_New_Nonzeroed(Py_ssize_t size)
{
PyTupleObject *op;
if (size == 0) {
return tuple_get_empty();
}
op = tuple_alloc(size);
if (op == NULL) {
return NULL;
}
_PyObject_GC_TRACK(op);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bad practice: uninitialized tuples must be not tracked by the GC, it should only be tracked once all items are initialized. See #59313 I added _PyType_AllocNoTrack() to attempt to fix a similar issue in a few functions.

PyTuple_Pack() doesn't have this design flaw.

return (PyObject *) op;
}

Py_ssize_t
PyTuple_Size(PyObject *op)
{
Expand Down
4 changes: 2 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2051,7 +2051,7 @@ mro_implementation(PyTypeObject *type)
*/
PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, 0));
Py_ssize_t k = PyTuple_GET_SIZE(base->tp_mro);
PyObject *result = PyTuple_New(k + 1);
PyObject *result = _PyTuple_New_Nonzeroed(k + 1);
if (result == NULL) {
return NULL;
}
Expand Down Expand Up @@ -5639,7 +5639,7 @@ reduce_newobj(PyObject *obj)
return NULL;
}
n = args ? PyTuple_GET_SIZE(args) : 0;
newargs = PyTuple_New(n+1);
newargs = _PyTuple_New_Nonzeroed(n+1);
if (newargs == NULL) {
Py_XDECREF(args);
Py_DECREF(newobj);
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2647,7 +2647,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(BUILD_TUPLE) {
PyObject *tup = PyTuple_New(oparg);
PyObject *tup = _PyTuple_New_Nonzeroed(oparg);
if (tup == NULL)
goto error;
while (--oparg >= 0) {
Expand Down Expand Up @@ -5937,7 +5937,7 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
allargs = args;
}
else {
kwnames = PyTuple_New(kwcount);
kwnames = _PyTuple_New_Nonzeroed(kwcount);
if (kwnames == NULL) {
goto fail;
}
Expand Down