chore: add a couple of moves in pybind11.h (#3941)

* Add missing std::moves in enum_base methods

* Remove useless move

* Add one more std::move in print

* Further optimize print

* Some more misc optimizations
This commit is contained in:
Aaron Gokaslan 2022-05-17 11:29:49 -04:00 committed by GitHub
parent dff6fa0554
commit a8b3ff30f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1926,7 +1926,8 @@ struct enum_base {
[](const object &arg) -> str { [](const object &arg) -> str {
handle type = type::handle_of(arg); handle type = type::handle_of(arg);
object type_name = type.attr("__name__"); object type_name = type.attr("__name__");
return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg)); return pybind11::str("<{}.{}: {}>")
.format(std::move(type_name), enum_name(arg), int_(arg));
}, },
name("__repr__"), name("__repr__"),
is_method(m_base)); is_method(m_base));
@ -1936,7 +1937,7 @@ struct enum_base {
m_base.attr("__str__") = cpp_function( m_base.attr("__str__") = cpp_function(
[](handle arg) -> str { [](handle arg) -> str {
object type_name = type::handle_of(arg).attr("__name__"); object type_name = type::handle_of(arg).attr("__name__");
return pybind11::str("{}.{}").format(type_name, enum_name(arg)); return pybind11::str("{}.{}").format(std::move(type_name), enum_name(arg));
}, },
name("name"), name("name"),
is_method(m_base)); is_method(m_base));
@ -2576,8 +2577,8 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) {
for (size_t i = 0; i < args.size(); ++i) { for (size_t i = 0; i < args.size(); ++i) {
strings[i] = str(args[i]); strings[i] = str(args[i]);
} }
auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" "); auto sep = kwargs.contains("sep") ? kwargs["sep"] : str(" ");
auto line = sep.attr("join")(strings); auto line = sep.attr("join")(std::move(strings));
object file; object file;
if (kwargs.contains("file")) { if (kwargs.contains("file")) {
@ -2596,7 +2597,7 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) {
auto write = file.attr("write"); auto write = file.attr("write");
write(line); write(line);
write(kwargs.contains("end") ? kwargs["end"] : cast("\n")); write(kwargs.contains("end") ? kwargs["end"] : str("\n"));
if (kwargs.contains("flush") && kwargs["flush"].cast<bool>()) { if (kwargs.contains("flush") && kwargs["flush"].cast<bool>()) {
file.attr("flush")(); file.attr("flush")();
@ -2639,7 +2640,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
function override = getattr(self, name, function()); function override = getattr(self, name, function());
if (override.is_cpp_function()) { if (override.is_cpp_function()) {
cache.insert(key); cache.insert(std::move(key));
return function(); return function();
} }