Skip to content

bpo-36030: Add an internal _PyTuple_NewUnsafe() function#11927

Closed
sir-sigurd wants to merge 1 commit into
python:masterfrom
sir-sigurd:tuple-unsafe
Closed

bpo-36030: Add an internal _PyTuple_NewUnsafe() function#11927
sir-sigurd wants to merge 1 commit into
python:masterfrom
sir-sigurd:tuple-unsafe

Conversation

@sir-sigurd

@sir-sigurd sir-sigurd commented Feb 19, 2019

Copy link
Copy Markdown
Contributor

https://bugs.python.org/issue36030

I'm going to update PR using new function in more cases when there is consensus about adding such function.

Comment thread Objects/tupleobject.c
}

PyObject *
_PyTuple_NewUnsafe(Py_ssize_t size)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if _PyTuple_NewNotInitialized is better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest the name _PyTuple_NewUninitialized.

@vstinner

Copy link
Copy Markdown
Member

@sir-sigurd wrote similar change for list: PR #8332.

Comment thread Objects/tupleobject.c
PyObject *
PyTuple_New(Py_ssize_t size)
static PyObject *
tuple_new_internal(Py_ssize_t size, int initialize)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Uninitialized tuples must be initialized with Py_SIZE(op) = 0 since they are tracked by the GC (_PyObject_GC_TRACK call below). A GC collection can be triggered while the tuple is filled, whereas tupletraverse() uses Py_SIZE() to iterate on items. If Py_SIZE() isn't 0, tupletraverse() will likely crash.

I suggest the function name "tuple_new_impl".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Uninitialized tuples must be initialized with Py_SIZE(op) = 0 since they are tracked by the GC (_PyObject_GC_TRACK call below). A GC collection can be triggered while the tuple is filled, whereas tupletraverse() uses Py_SIZE() to iterate on items. If Py_SIZE() isn't 0, tupletraverse() will likely crash.

I don't like setting Py_SIZE(op) = 0 and I see these alternatives:

  1. if I understand correctly GC collection can be triggered only when some objects are Py_DECREFed, if that's true we can limit usage of proposed function only to the cases when it's quite obvious that there are no Py_DECREFs until tuple is filled (it's done this way in bpo-34151: Improve performance of some list operations #8332), in the current patch there are just 2 not so obvious cases when PyDict_Next() is used
  2. call _PyObject_GC_TRACK() in caller when tuple is filled as you proposed in bpo-34151: Improve performance of some list operations #8332 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I suggest the function name "tuple_new_impl".

There is already this one:

static PyObject *
tuple_new_impl(PyTypeObject *type, PyObject *iterable)
/*[clinic end generated code: output=4546d9f0d469bce7 input=86963bcde633b5a2]*/
{
if (type != &PyTuple_Type)
return tuple_subtype_new(type, iterable);
if (iterable == NULL)
return PyTuple_New(0);
else
return PySequence_Tuple(iterable);
}

Comment thread Objects/tupleobject.c
}

PyObject *
_PyTuple_NewUnsafe(Py_ssize_t size)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest the name _PyTuple_NewUninitialized.

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants