maint(perf): Optimize Numpy constructor to remove copies by value. (#3183)

* maint(perf): Optimize Numpy Constructor with additional std::move

* Add more moves
This commit is contained in:
Aaron Gokaslan 2021-08-09 12:48:27 -04:00 committed by GitHub
parent 61ee923bb1
commit ff590c1258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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