From a8b3ff30f9649459021adc80f98a945d3ac675a5 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 17 May 2022 11:29:49 -0400 Subject: [PATCH] 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 --- include/pybind11/pybind11.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index cd86152e5..ca94aaeb3 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1926,7 +1926,8 @@ struct enum_base { [](const object &arg) -> str { handle type = type::handle_of(arg); 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__"), is_method(m_base)); @@ -1936,7 +1937,7 @@ struct enum_base { m_base.attr("__str__") = cpp_function( [](handle arg) -> str { 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"), 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) { strings[i] = str(args[i]); } - auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" "); - auto line = sep.attr("join")(strings); + auto sep = kwargs.contains("sep") ? kwargs["sep"] : str(" "); + auto line = sep.attr("join")(std::move(strings)); object file; if (kwargs.contains("file")) { @@ -2596,7 +2597,7 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) { auto write = file.attr("write"); 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()) { 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()); if (override.is_cpp_function()) { - cache.insert(key); + cache.insert(std::move(key)); return function(); }