mirror of
https://github.com/RYDE-WORK/pybind11.git
synced 2026-01-30 19:23:12 +08:00
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 126fc7c524ea7a51b54720defd75de3470d69557. * 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
This commit is contained in:
parent
f2f0c69096
commit
c4e295287b
@ -1243,8 +1243,8 @@ struct arg_v : arg {
|
|||||||
private:
|
private:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
arg_v(arg &&base, T &&x, const char *descr = nullptr)
|
arg_v(arg &&base, T &&x, const char *descr = nullptr)
|
||||||
: arg(base), value(reinterpret_steal<object>(
|
: arg(base), value(reinterpret_steal<object>(detail::make_caster<T>::cast(
|
||||||
detail::make_caster<T>::cast(x, return_value_policy::automatic, {}))),
|
std::forward<T>(x), return_value_policy::automatic, {}))),
|
||||||
descr(descr)
|
descr(descr)
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
,
|
,
|
||||||
@ -1491,7 +1491,7 @@ private:
|
|||||||
type_id<T>());
|
type_id<T>());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
args_list.append(o);
|
args_list.append(std::move(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(list &args_list, detail::args_proxy ap) {
|
void process(list &args_list, detail::args_proxy ap) {
|
||||||
|
|||||||
@ -640,9 +640,9 @@ private:
|
|||||||
|
|
||||||
list names, formats, offsets;
|
list names, formats, offsets;
|
||||||
for (auto &descr : field_descriptors) {
|
for (auto &descr : field_descriptors) {
|
||||||
names.append(descr.name);
|
names.append(std::move(descr.name));
|
||||||
formats.append(descr.format);
|
formats.append(std::move(descr.format));
|
||||||
offsets.append(descr.offset);
|
offsets.append(std::move(descr.offset));
|
||||||
}
|
}
|
||||||
return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize);
|
return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -268,10 +268,7 @@ public:
|
|||||||
/// Copy constructor; always increases the reference count
|
/// Copy constructor; always increases the reference count
|
||||||
object(const object &o) : handle(o) { inc_ref(); }
|
object(const object &o) : handle(o) { inc_ref(); }
|
||||||
/// Move constructor; steals the object from ``other`` and preserves its reference count
|
/// Move constructor; steals the object from ``other`` and preserves its reference count
|
||||||
object(object &&other) noexcept {
|
object(object &&other) noexcept : handle(other) { other.m_ptr = nullptr; }
|
||||||
m_ptr = other.m_ptr;
|
|
||||||
other.m_ptr = nullptr;
|
|
||||||
}
|
|
||||||
/// Destructor; automatically calls `handle::dec_ref()`
|
/// Destructor; automatically calls `handle::dec_ref()`
|
||||||
~object() { dec_ref(); }
|
~object() { dec_ref(); }
|
||||||
|
|
||||||
@ -1519,8 +1516,8 @@ private:
|
|||||||
class slice : public object {
|
class slice : public object {
|
||||||
public:
|
public:
|
||||||
PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
|
PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
|
||||||
slice(handle start, handle stop, handle step) {
|
slice(handle start, handle stop, handle step)
|
||||||
m_ptr = PySlice_New(start.ptr(), stop.ptr(), step.ptr());
|
: object(PySlice_New(start.ptr(), stop.ptr(), step.ptr()), stolen_t{}) {
|
||||||
if (!m_ptr) {
|
if (!m_ptr) {
|
||||||
pybind11_fail("Could not allocate slice object!");
|
pybind11_fail("Could not allocate slice object!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -79,7 +79,7 @@ struct set_caster {
|
|||||||
for (auto &&value : src) {
|
for (auto &&value : src) {
|
||||||
auto value_ = reinterpret_steal<object>(
|
auto value_ = reinterpret_steal<object>(
|
||||||
key_conv::cast(forward_like<T>(value), policy, parent));
|
key_conv::cast(forward_like<T>(value), policy, parent));
|
||||||
if (!value_ || !s.add(value_)) {
|
if (!value_ || !s.add(std::move(value_))) {
|
||||||
return handle();
|
return handle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user