On Tue, 23 Jun 2020 11:18:37 +0200
Victor Stinner
Pseudo-code:
void iterate(PyObject *obj) { PyObjectPP_View view;
if (PyObjectPP_View_Get(&view, obj)) { // fast-path: the object provides a PyObject** view for (Py_ssize_t i=0; i < view.len; i++ { PyObject *item = view.array[i]; ... } PyObjectPP_View_Release(&view); } else { // slow code path using PySequence_GetItem() or anything else ... }
It is quite cumbersome for extension code to have to re-implement all this by hand. Instead, it would be nice to have a "Visit" primitive so that one can write e.g.: void iterate(PyObject* obj) { Py_VisitObjectSequence([&](PyObject* item) { // ... }); } The above is a C++ lambda function (a closure, actually). The C spelling would be less nice, and you'd have to ensure that it is still performant (i.e., that the visitor is inlined inside the iteration loop - at least in release builds). (I called it Py_VisitObjectSequence so that you can also have Py_VisitIntSequence, Py_VisitFloatSequence, etc.) Regards Antoine.