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
{{ message }}
Repository navigation
[PERF]: Improve conversion of Python sequences to C++ vectors #1644
Conversion of Python sequences to C++ vectors currently looks like:
cdef vector[cydriver.CUatomicOperation] cyoperations = [int(pyoperations) for pyoperations in (operations)]
This has a number of suboptimal things:
It creates a temporary Python list (which may be realloc'd multiple times as it grows) that is totally unnecessary.
List comprehensions are significantly slower than for loops in Cython, since the former requires making multiple Python-level calls to an iterator object.
Instead, we know the length of the sequence in advance, so we can resize or pre-allocate the vector of the correct size, and then fill it with a for loop.
An example as to where this pattern is used is in cuModuleLoadDataEx. However, even with a sequence of ~100 elements, I can't seem to get this change to move the needle. So closing as ineffective.
Activity
mdboom commentedon Feb 19, 2026
An example as to where this pattern is used is in
cuModuleLoadDataEx. However, even with a sequence of ~100 elements, I can't seem to get this change to move the needle. So closing as ineffective.jakirkham commentedon Feb 20, 2026
TBH it is possible that the most succinct form is also the most efficient form
cdef vector[cydriver.CUatomicOperation] cyoperations = operationsAt least looking at a similar case
cdef vector[Py_ssize_t] cy_range_to_vector(Py_ssize_t N): cdef vector[Py_ssize_t] L = range(N) return LCython generates code from this template, which does what you describe above
mdboom commentedon Feb 20, 2026
Thanks for that @jakirkham. With your suggestion, I can shave off 100ns. Not nothing, especially since it actually simplifies our code.
Fix NVIDIA#1644: Faster conversion of sequence of enum to vector
Fix NVIDIA#1644: Faster conversion of sequence of enum to vector (NVI…
jakirkham commentedon Feb 20, 2026
Happy to help 🙂