Closed
Description
When constructing a std::vector using a Python buffer protocol object, there is a simple optimization for step == 1 (i.e. a simple contiguous array) that can yield a significant performance boost, especially for large arrays.
Old code:
auto vec = std::unique_ptr<Vector>(new Vector());
vec->reserve((size_t) info.shape[0]);
T *p = static_cast<T*>(info.ptr);
ssize_t step = info.strides[0] / static_cast<ssize_t>(sizeof(T));
T *end = p + info.shape[0] * step;
for (; p != end; p += step)
vec->push_back(*p);
return vec.release();
New code:
T *p = static_cast<T*>(info.ptr);
ssize_t step = info.strides[0] / static_cast<ssize_t>(sizeof(T));
T *end = p + info.shape[0] * step;
if (step == 1) {
auto vec = std::make_unique<Vector>(p, end);
return vec.release();
}
else {
auto vec = std::make_unique<Vector>();
vec->reserve((size_t) info.shape[0]);
for (; p != end; p += step)
vec->push_back(*p);
return vec.release();
}
Metadata
Metadata
Assignees
Labels
No labels