[QUESTION]: Speedup for converting numpy array to std::vector<T>
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
Problem description
I have a function foo which takes a std::vector as input, and I would like to expose the function to python, in which python programmers would call foo by passing in python lists or numpy arrays.
extern void foo(const std::vector<int64_t> &v);
PYBIND11_MODULE(test, m) {
m.def("foo", &foo);
}
For numpy arrays, the performance of casting is a bit slower than I thought: in fact, it is even slower than converting a list to std::vector. After a bit of research, I found that to cast numpy arrays, one first converts numpy raw data to a PyObject (note for lists, this proc could be ignored) before converting it to T, and finally moves it to std::vector. For references, list_caster::load type_cast::load
So my question is, is this possible for a speed-up when casting a numpy array into std::vector? I have noticed that vector_buffer_impl does a pretty good job at converting a py::buffer to a std::vector, maybe this piece of code could be used when casting into std::vector. If this could be a way, I would volunteer to submit a pull request.
Any thoughts would be welcomed and appreciated. Thanks.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing list_caster::load and type_caster::load in include/pybind11/stl.h and cast.h, then compare them with vector_buffer_impl in include/pybind11/stl_bind.h. Benchmark NumPy-array and list conversion paths, and determine whether a direct buffer conversion is viable; done means the performance question has a reproducible result and a clearly scoped outcome.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- backend-api-design, performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100