You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But I'm not convinced myself by this approach. A more promising approach may be to support PyObject* type in the Py_buffer protocol. Apparently, some projects have an unofficial support for that.
I think this could be solved with a "sequence unpacking API" that would return an array of owned refs:
/* * Try to unpack `nitems` items from `seq`, starting at position `start`, into `items`. * * Returns the number of items actually unpacked: * - is less than `nitems` if sequence is too short; * - is 0 if past sequence end; * - is -1 on error. * * The unpacked items are all owned references and must be released * with `PySequence_ClearUnpacked` when done. */Py_ssize_tPySequence_Unpack(PyObject*seq, Py_ssize_tstart, Py_ssize_tnitems, PyObject**items);
voidPySequence_ClearUnpacked(PyObject**items, Py_ssize_tstart, Py_ssize_tnitems);
Example usage:
// Iterate over the items in sequence `seq`, in 128-element batches#defineBATCH_SIZE 128
PyObject*items[BATCH_SIZE];
Py_ssize_tstart=0, nitems;
while ((nitems=PySequence_Unpack(seq, start, BATCH_SIZE, items)) >0) {
// Do something with the `nitems` objects in `items`...
...
// Clear owned refsPySequence_ClearUnpacked(items, start, nitems);
// Move to next batchstart+=nitems;
}
if (nitems<0) {
// PySequence_Unpack failed, handle error
...
}
Batching solves the performance problem of calling PySequence_GetItem one item at a time, but without compromising with error handling and safety.
A more promising approach may be to support PyObject* type in the Py_buffer protocol. Apparently, some projects have an unofficial support for that.
This is already part of PEP 3118 (see the O typecode for Python objects), and has been supported by NumPy for a long time.
But I don't think using the buffer protocol is adequate for quick sequence access over potentially short sequences. The use cases are different. Also, this would require tuple and list to implement the buffer protocol which is potentially disruptive.
Yeah, batching seems to be preferred over my PyResource API based on "create a single view, use the view, delete the view" knowning that the creation could require to copy the whole array, create new strong references, and then decrement ref count.
Activity
vstinner commentedon Oct 13, 2023
That was a request from PyPy devs. By the way, the specific case of Py_buffer was fixed in Python 3.12 by PEP 688 – Making the buffer protocol accessible in Python.
I propose adding a PyResource API:
But I'm not convinced myself by this approach. A more promising approach may be to support
PyObject*type in the Py_buffer protocol. Apparently, some projects have an unofficial support for that.pitrou commentedon Oct 14, 2023
I think this could be solved with a "sequence unpacking API" that would return an array of owned refs:
Example usage:
Batching solves the performance problem of calling
PySequence_GetItemone item at a time, but without compromising with error handling and safety.pitrou commentedon Oct 14, 2023
This is already part of PEP 3118 (see the
Otypecode for Python objects), and has been supported by NumPy for a long time.But I don't think using the buffer protocol is adequate for quick sequence access over potentially short sequences. The use cases are different. Also, this would require
tupleandlistto implement the buffer protocol which is potentially disruptive.vstinner commentedon Oct 14, 2023
Yeah, batching seems to be preferred over my PyResource API based on "create a single view, use the view, delete the view" knowning that the creation could require to copy the whole array, create new strong references, and then decrement ref count.