[BUG] Creating fixed size list
by multiplication is not optimised
#3922
Labels
Milestone
list
by multiplication is not optimised
#3922
It would be useful to have an efficient way to construct a fixed sized
list
using Python-like code in Cython.Is your feature request related to a problem? Please describe.
For example, one might have the following code in Python...
Though once this is compiled, it appears to coerce
n
to a Pythonint
, create a singletonlist
withNone
, and useNumber
based multiplication betweenn
and the singletonlist
. Preferably this would allocate alist
of sizen
and then fill it withNone
in a Cfor
-loop.Describe the solution you'd like
Instead of generating code that relies on
append
or handling things at the Python level, it would be helpful to have some code roughly like this (error handling and refcounting skipped for simplicity).Describe alternatives you've considered
One could try to use generators, though they don't look at the size hint as noted in issue ( #1311 ) and issue ( #2844 ). Plus these wind up being less performant than this implementation is in pure Python. Though Cython performance is likely fine with the other approach. Perhaps if Cython reinterpreted
n * [None]
as[None for i in n]
, this could benefit from whatever solution is determined for the generator issues noted before.Additional context
This comes up periodically when one wants to quickly preallocate a fixed sized Python
list
and some other alternative like an array wouldn't otherwise work (for example needing to hold arbitrary Pythonobject
s and/or user expecting alist
to bereturn
ed).The text was updated successfully, but these errors were encountered: