fix(clang-tidy): performance fixes applied in tests and CI (#3051)

* Initial fixes

* Whoops

* Finish clang-tidy manual fixes

* Add two missing fixes

* Revert

* Update clang-tidy

* Try to fix unreachable code error

* Move nolint comment

* Apply missing fix

* Don't override clang-tidy config

* Does this fix clang-tidy?

* Make all clang-tidy errors visible

* Add comments about NOLINTs and remove a few

* Fix typo
This commit is contained in:
Aaron Gokaslan
2021-06-22 12:11:54 -04:00
committed by GitHub
parent 3b30b0a51e
commit dac74ebdf5
36 changed files with 664 additions and 431 deletions

View File

@@ -13,6 +13,7 @@
#include <pybind11/stl.h>
#include <cstdint>
#include <utility>
// Size / dtype checks.
struct DtypeCheck {
@@ -192,7 +193,7 @@ TEST_SUBMODULE(numpy_array, sm) {
sm.def("scalar_int", []() { return py::array(py::dtype("i"), {}, {}, &data_i); });
// test_wrap
sm.def("wrap", [](py::array a) {
sm.def("wrap", [](const py::array &a) {
return py::array(
a.dtype(),
{a.shape(), a.shape() + a.ndim()},
@@ -222,8 +223,10 @@ TEST_SUBMODULE(numpy_array, sm) {
// test_isinstance
sm.def("isinstance_untyped", [](py::object yes, py::object no) {
return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no);
return py::isinstance<py::array>(std::move(yes))
&& !py::isinstance<py::array>(std::move(no));
});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("isinstance_typed", [](py::object o) {
return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
});
@@ -236,7 +239,7 @@ TEST_SUBMODULE(numpy_array, sm) {
"array_t<double>"_a=py::array_t<double>()
);
});
sm.def("converting_constructors", [](py::object o) {
sm.def("converting_constructors", [](const py::object &o) {
return py::dict(
"array"_a=py::array(o),
"array_t<int32>"_a=py::array_t<std::int32_t>(o),
@@ -245,39 +248,59 @@ TEST_SUBMODULE(numpy_array, sm) {
});
// test_overload_resolution
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<double>) { return "double"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<float>) { return "float"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<int>) { return "int"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<unsigned short>) { return "unsigned short"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<long long>) { return "long long"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<std::complex<double>>) { return "double complex"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<std::complex<float>>) { return "float complex"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<std::complex<double>>) { return "double complex"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<double>) { return "double"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<std::complex<float>>) { return "float complex"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<float>) { return "float"; });
// [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
// Only accept the exact types:
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded3", [](py::array_t<int>) { return "int"; }, py::arg{}.noconvert());
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded3", [](py::array_t<double>) { return "double"; }, py::arg{}.noconvert());
// Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but
// rather that float gets converted via the safe (conversion to double) overload:
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded4", [](py::array_t<long long, 0>) { return "long long"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded4", [](py::array_t<double, 0>) { return "double"; });
// But we do allow conversion to int if forcecast is enabled (but only if no overload matches
// without conversion)
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded5", [](py::array_t<double>) { return "double"; });
// test_greedy_string_overload
// Issue 685: ndarray shouldn't go to std::string overload
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("issue685", [](std::string) { return "string"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("issue685", [](py::array) { return "array"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("issue685", [](py::object) { return "other"; });
// test_array_unchecked_fixed_dims
@@ -306,7 +329,7 @@ TEST_SUBMODULE(numpy_array, sm) {
r(i, j, k) = start++;
return a;
});
sm.def("proxy_squared_L2_norm", [](py::array_t<double> a) {
sm.def("proxy_squared_L2_norm", [](const py::array_t<double> &a) {
auto r = a.unchecked<1>();
double sumsq = 0;
for (py::ssize_t i = 0; i < r.shape(0); i++)
@@ -396,51 +419,78 @@ TEST_SUBMODULE(numpy_array, sm) {
return a;
});
sm.def("index_using_ellipsis", [](py::array a) {
return a[py::make_tuple(0, py::ellipsis(), 0)];
});
sm.def("index_using_ellipsis",
[](const py::array &a) { return a[py::make_tuple(0, py::ellipsis(), 0)]; });
// test_argument_conversions
sm.def("accept_double",
[](py::array_t<double, 0>) {},
py::arg("a"));
sm.def("accept_double_forcecast",
[](py::array_t<double, py::array::forcecast>) {},
py::arg("a"));
sm.def("accept_double_c_style",
[](py::array_t<double, py::array::c_style>) {},
py::arg("a"));
sm.def("accept_double_c_style_forcecast",
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
py::arg("a"));
sm.def("accept_double_f_style",
[](py::array_t<double, py::array::f_style>) {},
py::arg("a"));
sm.def("accept_double_f_style_forcecast",
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
py::arg("a"));
sm.def("accept_double_noconvert",
[](py::array_t<double, 0>) {},
"a"_a.noconvert());
sm.def("accept_double_forcecast_noconvert",
[](py::array_t<double, py::array::forcecast>) {},
"a"_a.noconvert());
sm.def("accept_double_c_style_noconvert",
[](py::array_t<double, py::array::c_style>) {},
"a"_a.noconvert());
sm.def("accept_double_c_style_forcecast_noconvert",
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
"a"_a.noconvert());
sm.def("accept_double_f_style_noconvert",
[](py::array_t<double, py::array::f_style>) {},
"a"_a.noconvert());
sm.def("accept_double_f_style_forcecast_noconvert",
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
"a"_a.noconvert());
sm.def(
"accept_double",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, 0>) {},
py::arg("a"));
sm.def(
"accept_double_forcecast",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast>) {},
py::arg("a"));
sm.def(
"accept_double_c_style",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::c_style>) {},
py::arg("a"));
sm.def(
"accept_double_c_style_forcecast",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
py::arg("a"));
sm.def(
"accept_double_f_style",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::f_style>) {},
py::arg("a"));
sm.def(
"accept_double_f_style_forcecast",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
py::arg("a"));
sm.def(
"accept_double_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, 0>) {},
"a"_a.noconvert());
sm.def(
"accept_double_forcecast_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast>) {},
"a"_a.noconvert());
sm.def(
"accept_double_c_style_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::c_style>) {},
"a"_a.noconvert());
sm.def(
"accept_double_c_style_forcecast_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
"a"_a.noconvert());
sm.def(
"accept_double_f_style_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::f_style>) {},
"a"_a.noconvert());
sm.def(
"accept_double_f_style_forcecast_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
"a"_a.noconvert());
// Check that types returns correct npy format descriptor
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_float", [](py::array_t<float>) {});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_double", [](py::array_t<double>) {});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_const_float", [](py::array_t<const float>) {});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_const_double", [](py::array_t<const double>) {});
}