From 79178e713d8f3ac3ae5b77017eb57f1bc67f336f Mon Sep 17 00:00:00 2001 From: jbarlow83 Date: Fri, 18 Jun 2021 07:17:34 -0700 Subject: [PATCH 1/2] fix(setup_helpers): try import multiprocessing.synchronize too (#3043) --- pybind11/setup_helpers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 84a4584c5..1050f6125 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -410,7 +410,9 @@ class ParallelCompile(object): compiler._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) try: - import multiprocessing + # Importing .synchronize checks for platforms that have some multiprocessing + # capabilities but lack semaphores, such as AWS Lambda and Android Termux. + import multiprocessing.synchronize from multiprocessing.pool import ThreadPool except ImportError: threads = 1 From af6218ff78d480c5e93237174ee6f02ec36853cc Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 19 Jun 2021 13:53:27 -0400 Subject: [PATCH 2/2] fix(clang-tidy): Apply performance fixes from clang-tidy (#3046) * Apply performance fixes from clang-tidy * 2nd Round of Perf Optimizations * 3rd round of fixes & handle false-positive * Apply missing fix and clang-format * Apply reviewer comment --- .clang-tidy | 4 +++ include/pybind11/buffer_info.h | 6 ++-- include/pybind11/cast.h | 11 ++++--- include/pybind11/eval.h | 8 +++-- include/pybind11/iostream.h | 41 +++++++++++------------- include/pybind11/numpy.h | 4 +-- include/pybind11/pybind11.h | 58 +++++++++++++++++++--------------- include/pybind11/pytypes.h | 4 +-- include/pybind11/stl.h | 11 ++++--- include/pybind11/stl_bind.h | 49 ++++++++++++++-------------- 10 files changed, 105 insertions(+), 91 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index e29d92989..3079b3b3c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -10,4 +10,8 @@ modernize-use-auto, modernize-use-emplace, ' +CheckOptions: +- key: performance-unnecessary-value-param.AllowedTypes + value: 'exception_ptr$;' + HeaderFilterRegex: 'pybind11/.*h' diff --git a/include/pybind11/buffer_info.h b/include/pybind11/buffer_info.h index d803004a1..47dc39d4e 100644 --- a/include/pybind11/buffer_info.h +++ b/include/pybind11/buffer_info.h @@ -91,11 +91,9 @@ struct buffer_info { buffer_info(const buffer_info &) = delete; buffer_info& operator=(const buffer_info &) = delete; - buffer_info(buffer_info &&other) { - (*this) = std::move(other); - } + buffer_info(buffer_info &&other) noexcept { (*this) = std::move(other); } - buffer_info& operator=(buffer_info &&rhs) { + buffer_info &operator=(buffer_info &&rhs) noexcept { ptr = rhs.ptr; itemsize = rhs.itemsize; size = rhs.size; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index f68a35bfa..1ef1a9ce9 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1064,7 +1064,9 @@ struct kw_only {}; struct pos_only {}; template -arg_v arg::operator=(T &&value) const { return {std::move(*this), std::forward(value)}; } +arg_v arg::operator=(T &&value) const { + return {*this, std::forward(value)}; +} /// Alias for backward compatibility -- to be removed in version 2.0 template using arg_t = arg_v; @@ -1286,7 +1288,7 @@ private: "may be passed via py::arg() to a python function call. " "(compile in debug mode for details)"); } - [[noreturn]] static void nameless_argument_error(std::string type) { + [[noreturn]] static void nameless_argument_error(const std::string &type) { throw type_error("Got kwargs without a name of type '" + type + "'; only named " "arguments may be passed via py::arg() to a python function call. "); } @@ -1295,7 +1297,7 @@ private: "(compile in debug mode for details)"); } - [[noreturn]] static void multiple_values_error(std::string name) { + [[noreturn]] static void multiple_values_error(const std::string &name) { throw type_error("Got multiple values for keyword argument '" + name + "'"); } @@ -1304,7 +1306,8 @@ private: "(compile in debug mode for details)"); } - [[noreturn]] static void argument_cast_error(std::string name, std::string type) { + [[noreturn]] static void argument_cast_error(const std::string &name, + const std::string &type) { throw cast_error("Unable to convert call argument '" + name + "' of type '" + type + "' to Python object"); } diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index fa6b8af47..bf06504a3 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -11,6 +11,8 @@ #pragma once +#include + #include "pybind11.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -43,7 +45,7 @@ enum eval_mode { }; template -object eval(str expr, object global = globals(), object local = object()) { +object eval(const str &expr, object global = globals(), object local = object()) { if (!local) local = global; @@ -75,8 +77,8 @@ object eval(const char (&s)[N], object global = globals(), object local = object return eval(expr, global, local); } -inline void exec(str expr, object global = globals(), object local = object()) { - eval(expr, global, local); +inline void exec(const str &expr, object global = globals(), object local = object()) { + eval(expr, std::move(global), std::move(local)); } template diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index 7c1c718b0..6a793eadc 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -11,14 +11,15 @@ #include "pybind11.h" -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -117,11 +118,8 @@ private: } public: - - pythonbuf(object pyostream, size_t buffer_size = 1024) - : buf_size(buffer_size), - d_buffer(new char[buf_size]), - pywrite(pyostream.attr("write")), + pythonbuf(const object &pyostream, size_t buffer_size = 1024) + : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")), pyflush(pyostream.attr("flush")) { setp(d_buffer.get(), d_buffer.get() + buf_size - 1); } @@ -168,9 +166,8 @@ protected: detail::pythonbuf buffer; public: - scoped_ostream_redirect( - std::ostream &costream = std::cout, - object pyostream = module_::import("sys").attr("stdout")) + scoped_ostream_redirect(std::ostream &costream = std::cout, + const object &pyostream = module_::import("sys").attr("stdout")) : costream(costream), buffer(pyostream) { old = costream.rdbuf(&buffer); } @@ -199,10 +196,9 @@ public: \endrst */ class scoped_estream_redirect : public scoped_ostream_redirect { public: - scoped_estream_redirect( - std::ostream &costream = std::cerr, - object pyostream = module_::import("sys").attr("stderr")) - : scoped_ostream_redirect(costream,pyostream) {} + scoped_estream_redirect(std::ostream &costream = std::cerr, + const object &pyostream = module_::import("sys").attr("stderr")) + : scoped_ostream_redirect(costream, pyostream) {} }; @@ -261,9 +257,10 @@ PYBIND11_NAMESPACE_END(detail) m.noisy_function_with_error_printing() \endrst */ -inline class_ add_ostream_redirect(module_ m, std::string name = "ostream_redirect") { - return class_(m, name.c_str(), module_local()) - .def(init(), arg("stdout")=true, arg("stderr")=true) +inline class_ +add_ostream_redirect(module_ m, const std::string &name = "ostream_redirect") { + return class_(std::move(m), name.c_str(), module_local()) + .def(init(), arg("stdout") = true, arg("stderr") = true) .def("__enter__", &detail::OstreamRedirect::enter) .def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); }); } diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index ec4c53b43..928784246 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -164,10 +164,10 @@ struct npy_api { NPY_ULONG_, NPY_ULONGLONG_, NPY_UINT_), }; - typedef struct { + struct PyArray_Dims { Py_intptr_t *ptr; int len; - } PyArray_Dims; + }; static npy_api& get() { static npy_api api = lookup(); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 754c875cd..2ff88dbf5 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1412,15 +1412,15 @@ public: template class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) { - cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)), - fset([pm](object, const D &value) { *pm = value; }, scope(*this)); + cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this)), + fset([pm](const object &, const D &value) { *pm = value; }, scope(*this)); def_property_static(name, fget, fset, return_value_policy::reference, extra...); return *this; } template class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) { - cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)); + cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this)); def_property_readonly_static(name, fget, return_value_policy::reference, extra...); return *this; } @@ -1671,30 +1671,36 @@ struct enum_base { }, name("__members__")), none(), none(), "" ); - #define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \ - m_base.attr(op) = cpp_function( \ - [](object a, object b) { \ - if (!type::handle_of(a).is(type::handle_of(b))) \ - strict_behavior; \ - return expr; \ - }, \ - name(op), is_method(m_base), arg("other")) +#define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \ + m_base.attr(op) = cpp_function( \ + [](const object &a, const object &b) { \ + if (!type::handle_of(a).is(type::handle_of(b))) \ + strict_behavior; \ + return expr; \ + }, \ + name(op), \ + is_method(m_base), \ + arg("other")) - #define PYBIND11_ENUM_OP_CONV(op, expr) \ - m_base.attr(op) = cpp_function( \ - [](object a_, object b_) { \ - int_ a(a_), b(b_); \ - return expr; \ - }, \ - name(op), is_method(m_base), arg("other")) +#define PYBIND11_ENUM_OP_CONV(op, expr) \ + m_base.attr(op) = cpp_function( \ + [](const object &a_, const object &b_) { \ + int_ a(a_), b(b_); \ + return expr; \ + }, \ + name(op), \ + is_method(m_base), \ + arg("other")) - #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ - m_base.attr(op) = cpp_function( \ - [](object a_, object b) { \ - int_ a(a_); \ - return expr; \ - }, \ - name(op), is_method(m_base), arg("other")) +#define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ + m_base.attr(op) = cpp_function( \ + [](const object &a_, const object &b) { \ + int_ a(a_); \ + return expr; \ + }, \ + name(op), \ + is_method(m_base), \ + arg("other")) if (is_convertible) { PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b)); @@ -2054,7 +2060,7 @@ exception ®ister_exception(handle scope, } PYBIND11_NAMESPACE_BEGIN(detail) -PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { +PYBIND11_NOINLINE inline void print(const tuple &args, const dict &kwargs) { auto strings = tuple(args.size()); for (size_t i = 0; i < args.size(); ++i) { strings[i] = str(args[i]); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 6a1d8356a..d1d92af6a 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -503,7 +503,7 @@ class accessor : public object_api> { public: accessor(handle obj, key_type key) : obj(obj), key(std::move(key)) { } accessor(const accessor &) = default; - accessor(accessor &&) = default; + accessor(accessor &&) noexcept = default; // accessor overload required to override default assignment operator (templates are not allowed // to replace default compiler-generated assignments). @@ -1508,7 +1508,7 @@ public: detail::any_container shape, detail::any_container strides) { return memoryview::from_buffer( - const_cast(ptr), itemsize, format, shape, strides, true); + const_cast(ptr), itemsize, format, std::move(shape), std::move(strides), true); } template diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 18fbafb1e..61d3fba61 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -159,10 +159,13 @@ template struct list_caster { } private: - template ().reserve(0)), void>::value, int> = 0> - void reserve_maybe(sequence s, Type *) { value.reserve(s.size()); } - void reserve_maybe(sequence, void *) { } + template < + typename T = Type, + enable_if_t().reserve(0)), void>::value, int> = 0> + void reserve_maybe(const sequence &s, Type *) { + value.reserve(s.size()); + } + void reserve_maybe(const sequence &, void *) {} public: template diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 83195ee49..b78bd27ce 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -128,11 +128,11 @@ void vector_modifiers(enable_if_t(new Vector()); v->reserve(len_hint(it)); for (handle h : it) - v->push_back(h.cast()); + v->push_back(h.cast()); return v.release(); })); @@ -151,27 +151,28 @@ void vector_modifiers(enable_if_t()); - } - } catch (const cast_error &) { - v.erase(v.begin() + static_cast(old_size), v.end()); - try { - v.shrink_to_fit(); - } catch (const std::exception &) { - // Do nothing - } - throw; - } - }, - arg("L"), - "Extend the list by appending all the items in the given list" - ); + cl.def( + "extend", + [](Vector &v, const iterable &it) { + const size_t old_size = v.size(); + v.reserve(old_size + len_hint(it)); + try { + for (handle h : it) { + v.push_back(h.cast()); + } + } catch (const cast_error &) { + v.erase(v.begin() + static_cast(old_size), + v.end()); + try { + v.shrink_to_fit(); + } catch (const std::exception &) { + // Do nothing + } + throw; + } + }, + arg("L"), + "Extend the list by appending all the items in the given list"); cl.def("insert", [](Vector &v, DiffType i, const T &x) { @@ -400,7 +401,7 @@ void vector_buffer_impl(Class_& cl, std::true_type) { return buffer_info(v.data(), static_cast(sizeof(T)), format_descriptor::format(), 1, {v.size()}, {sizeof(T)}); }); - cl.def(init([](buffer buf) { + cl.def(init([](const buffer &buf) { auto info = buf.request(); if (info.ndim != 1 || info.strides[0] % static_cast(sizeof(T))) throw type_error("Only valid 1D buffers can be copied to a vector");