Skip to content

Commit c4e2952

Browse files
authored
perf: Add more moves and optimize (#3845)
* Make slice constructor consistent * Add more missing std::move for ref steals * Add missing perfect forwarding for arg_v ctor * Add missing move in arg_v constructor * Revert "Add missing move in arg_v constructor" This reverts commit 126fc7c. * Add another missing move in cast.h * Optimize object move ctor * Don't do useless move * Make move ctor same as nb * Make obj move ctor same as nb * Revert changes which break MSVC
1 parent f2f0c69 commit c4e2952

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

include/pybind11/cast.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,8 +1243,8 @@ struct arg_v : arg {
12431243
private:
12441244
template <typename T>
12451245
arg_v(arg &&base, T &&x, const char *descr = nullptr)
1246-
: arg(base), value(reinterpret_steal<object>(
1247-
detail::make_caster<T>::cast(x, return_value_policy::automatic, {}))),
1246+
: arg(base), value(reinterpret_steal<object>(detail::make_caster<T>::cast(
1247+
std::forward<T>(x), return_value_policy::automatic, {}))),
12481248
descr(descr)
12491249
#if !defined(NDEBUG)
12501250
,
@@ -1491,7 +1491,7 @@ class unpacking_collector {
14911491
type_id<T>());
14921492
#endif
14931493
}
1494-
args_list.append(o);
1494+
args_list.append(std::move(o));
14951495
}
14961496

14971497
void process(list &args_list, detail::args_proxy ap) {

include/pybind11/numpy.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,9 @@ class dtype : public object {
640640

641641
list names, formats, offsets;
642642
for (auto &descr : field_descriptors) {
643-
names.append(descr.name);
644-
formats.append(descr.format);
645-
offsets.append(descr.offset);
643+
names.append(std::move(descr.name));
644+
formats.append(std::move(descr.format));
645+
offsets.append(std::move(descr.offset));
646646
}
647647
return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize);
648648
}

include/pybind11/pytypes.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,7 @@ class object : public handle {
268268
/// Copy constructor; always increases the reference count
269269
object(const object &o) : handle(o) { inc_ref(); }
270270
/// Move constructor; steals the object from ``other`` and preserves its reference count
271-
object(object &&other) noexcept {
272-
m_ptr = other.m_ptr;
273-
other.m_ptr = nullptr;
274-
}
271+
object(object &&other) noexcept : handle(other) { other.m_ptr = nullptr; }
275272
/// Destructor; automatically calls `handle::dec_ref()`
276273
~object() { dec_ref(); }
277274

@@ -1519,8 +1516,8 @@ class weakref : public object {
15191516
class slice : public object {
15201517
public:
15211518
PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
1522-
slice(handle start, handle stop, handle step) {
1523-
m_ptr = PySlice_New(start.ptr(), stop.ptr(), step.ptr());
1519+
slice(handle start, handle stop, handle step)
1520+
: object(PySlice_New(start.ptr(), stop.ptr(), step.ptr()), stolen_t{}) {
15241521
if (!m_ptr) {
15251522
pybind11_fail("Could not allocate slice object!");
15261523
}

include/pybind11/stl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct set_caster {
7979
for (auto &&value : src) {
8080
auto value_ = reinterpret_steal<object>(
8181
key_conv::cast(forward_like<T>(value), policy, parent));
82-
if (!value_ || !s.add(value_)) {
82+
if (!value_ || !s.add(std::move(value_))) {
8383
return handle();
8484
}
8585
}

0 commit comments

Comments
 (0)