From ff590c1258abf5b472f64b5ab337c5951922f81d Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 9 Aug 2021 12:48:27 -0400 Subject: [PATCH] maint(perf): Optimize Numpy constructor to remove copies by value. (#3183) * maint(perf): Optimize Numpy Constructor with additional std::move * Add more moves --- include/pybind11/numpy.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index aa8294e16..f0f3b60ee 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -478,11 +478,11 @@ public: dtype(list names, list formats, list offsets, ssize_t itemsize) { dict args; - args["names"] = names; - args["formats"] = formats; - args["offsets"] = offsets; + args["names"] = std::move(names); + args["formats"] = std::move(formats); + args["offsets"] = std::move(offsets); args["itemsize"] = pybind11::int_(itemsize); - m_ptr = from_args(args).release().ptr(); + m_ptr = from_args(std::move(args)).release().ptr(); } /// This is essentially the same as calling numpy.dtype(args) in Python. @@ -560,7 +560,7 @@ private: formats.append(descr.format); offsets.append(descr.offset); } - return dtype(names, formats, offsets, itemsize); + return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize); } }; @@ -1134,7 +1134,10 @@ inline PYBIND11_NOINLINE void register_structured_dtype( formats.append(field.descr); offsets.append(pybind11::int_(field.offset)); } - auto dtype_ptr = pybind11::dtype(names, formats, offsets, itemsize).release().ptr(); + auto dtype_ptr + = pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize) + .release() + .ptr(); // There is an existing bug in NumPy (as of v1.11): trailing bytes are // not encoded explicitly into the format string. This will supposedly