Skip to content

optimize buffer protocol constructor for stl_bind std::vector when buffer is a simple array #2102

Closed
@marc-chiesa

Description

@marc-chiesa

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions