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
In the cpython codebase PyTuple_Pack is used at various places (https://github.com/search?q=repo%3Apython%2Fcpython+PyTuple_Pack&type=code). The execution is not very fast as the implementation uses va_arg internally. For the 1- and 2 argument case we can improve performance by providing a direct implementation.
How large is the difference between PyTuple_Pack(), _PyTuple_FromArray(), PyTuple_Pack1() and PyTuple_Pack2()?
I afraid that it is a preliminary optimization. We need new API if:
It allows to express what was not possible with the old API. It is obvious.
It allows to avoid the overhead of more general API for common cases.
It is much more convenient in common cases. Usually it means less lines of code.
PyTuple_Pack() is already a convenient API in comparison with PyTuple_New() + Py_INCREF() + PyTuple_SET_ITEM(). And it is fast enough, faster than more general Py_BuildValue(), perhaps even faster than PyTuple_New() + Py_IncRef() + PyTuple_SetItem().
@serhiy-storchaka Thanks for the response. I agree the need for a new API seems slim. The only reason would be performance.
Here are some results: with the benchmark script from https://discuss.python.org/t/nwise-itertools/51718/17 we can compare the performance of the cpython pairwise (implemented in C) and a simple recipe (python only). With current main the python recipe is faster than the cpython C version.
So using PyTuple_Pack2 gives a performance improvement of 10-15%.
I will keep this issue open a few more days to see if there are different opinions or more use cases where the performance difference is important. If not I will close the issue and (perhaps) make a PR to update just the pairwise implementation with a direct PyTuple_New() + Py_INCREF() + PyTuple_SET_ITEM()
Looking through the cpython code there are only few places where the usage of PyTuple_Pack is performance critical. One had been adressed in #118219, another is addressed in #118703. Closing this issue
Activity
serhiy-storchaka commentedon Apr 30, 2024
How large is the difference between
PyTuple_Pack(),_PyTuple_FromArray(),PyTuple_Pack1()andPyTuple_Pack2()?I afraid that it is a preliminary optimization. We need new API if:
PyTuple_Pack()is already a convenient API in comparison withPyTuple_New()+Py_INCREF()+PyTuple_SET_ITEM(). And it is fast enough, faster than more generalPy_BuildValue(), perhaps even faster thanPyTuple_New()+Py_IncRef()+PyTuple_SetItem().eendebakpt commentedon Apr 30, 2024
@serhiy-storchaka Thanks for the response. I agree the need for a new API seems slim. The only reason would be performance.
Here are some results: with the benchmark script from https://discuss.python.org/t/nwise-itertools/51718/17 we can compare the performance of the cpython
pairwise(implemented in C) and a simple recipe (python only). With current main the python recipe is faster than the cpython C version.Main:
With
PyTuple_Pack2(implementation main...eendebakpt:cpython:putuple_pack2) the two versions are almost as fast:So using
PyTuple_Pack2gives a performance improvement of 10-15%.I will keep this issue open a few more days to see if there are different opinions or more use cases where the performance difference is important. If not I will close the issue and (perhaps) make a PR to update just the
pairwiseimplementation with a directPyTuple_New() + Py_INCREF() + PyTuple_SET_ITEM()eendebakpt commentedon May 7, 2024
Looking through the cpython code there are only few places where the usage of
PyTuple_Packis performance critical. One had been adressed in #118219, another is addressed in #118703. Closing this issue