Skip to content

Commit ff590c1

Browse files
authored
maint(perf): Optimize Numpy constructor to remove copies by value. (#3183)
* maint(perf): Optimize Numpy Constructor with additional std::move * Add more moves
1 parent 61ee923 commit ff590c1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

include/pybind11/numpy.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,11 @@ class dtype : public object {
478478

479479
dtype(list names, list formats, list offsets, ssize_t itemsize) {
480480
dict args;
481-
args["names"] = names;
482-
args["formats"] = formats;
483-
args["offsets"] = offsets;
481+
args["names"] = std::move(names);
482+
args["formats"] = std::move(formats);
483+
args["offsets"] = std::move(offsets);
484484
args["itemsize"] = pybind11::int_(itemsize);
485-
m_ptr = from_args(args).release().ptr();
485+
m_ptr = from_args(std::move(args)).release().ptr();
486486
}
487487

488488
/// This is essentially the same as calling numpy.dtype(args) in Python.
@@ -560,7 +560,7 @@ class dtype : public object {
560560
formats.append(descr.format);
561561
offsets.append(descr.offset);
562562
}
563-
return dtype(names, formats, offsets, itemsize);
563+
return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize);
564564
}
565565
};
566566

@@ -1134,7 +1134,10 @@ inline PYBIND11_NOINLINE void register_structured_dtype(
11341134
formats.append(field.descr);
11351135
offsets.append(pybind11::int_(field.offset));
11361136
}
1137-
auto dtype_ptr = pybind11::dtype(names, formats, offsets, itemsize).release().ptr();
1137+
auto dtype_ptr
1138+
= pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize)
1139+
.release()
1140+
.ptr();
11381141

11391142
// There is an existing bug in NumPy (as of v1.11): trailing bytes are
11401143
// not encoded explicitly into the format string. This will supposedly

0 commit comments

Comments
 (0)