Fix clang-tidy performance-* warnings in system/libhidl.
* Use const reference parameter type to avoid unnecessary copy.
* Use more efficient overloaded string methods.
* Use const reference type for loop index variables to avoid unnecessary copy.
Bug: 30407689
Bug: 30411878
Bug: 30413223
Test: build with WITH_TIDY=1
Test: libhidl_test
Change-Id: I1deae1f09d56c2fa9cd56728b13ca50305d18f3e
diff --git a/base/HidlInternal.cpp b/base/HidlInternal.cpp
index 5fae1c4..0ff52ad 100644
--- a/base/HidlInternal.cpp
+++ b/base/HidlInternal.cpp
@@ -96,7 +96,7 @@
instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
}
- for (auto path : instrumentationLibPaths) {
+ for (const auto& path : instrumentationLibPaths) {
DIR *dir = opendir(path.c_str());
if (dir == 0) {
LOG(WARNING) << path << " does not exist. ";
diff --git a/base/include/hidl/Status.h b/base/include/hidl/Status.h
index f812ebb..e2aab3f 100644
--- a/base/include/hidl/Status.h
+++ b/base/include/hidl/Status.h
@@ -143,7 +143,7 @@
void assertOk() const;
public:
return_status() {}
- return_status(Status s) : mStatus(s) {}
+ return_status(const Status& s) : mStatus(s) {}
return_status(const return_status &) = delete;
return_status &operator=(const return_status &) = delete;
@@ -240,7 +240,7 @@
template<> class Return<void> : public details::return_status {
public:
Return() : details::return_status() {}
- Return(Status s) : details::return_status(s) {}
+ Return(const Status& s) : details::return_status(s) {}
// move-able.
// precondition: "this" has checked status
diff --git a/test_main.cpp b/test_main.cpp
index b265607..90c8505 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -76,9 +76,9 @@
EXPECT_STREQ(s6.c_str(), "s6");
hidl_string s7 = std::string("s7"); // copy = from std::string
EXPECT_STREQ(s7.c_str(), "s7");
- hidl_string s8(s7); // copy constructor
+ hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor
EXPECT_STREQ(s8.c_str(), "s7");
- hidl_string s9 = s8; // copy =
+ hidl_string s9 = s8; // copy = // NOLINT, test the copy operator
EXPECT_STREQ(s9.c_str(), "s7");
char myCString[20] = "myCString";
s.setToExternal(&myCString[0], strlen(myCString));
@@ -154,14 +154,14 @@
using android::hardware::hidl_memory;
hidl_memory mem1 = hidl_memory(); // default constructor
- hidl_memory mem2 = mem1; // copy constructor (nullptr)
+ hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT
EXPECT_EQ(nullptr, mem2.handle());
native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
- hidl_memory mem4 = mem3; // copy constructor (regular handle)
+ hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT
EXPECT_EQ(mem3.name(), mem4.name());
EXPECT_EQ(mem3.size(), mem4.size());
@@ -169,7 +169,7 @@
EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
- hidl_memory mem6 = mem5;
+ hidl_memory mem6 = mem5; // NOLINT, test copying
EXPECT_EQ(nullptr, mem5.handle());
EXPECT_EQ(nullptr, mem6.handle());
}
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index a404d10..feb1254 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -93,7 +93,7 @@
}
ifs >> cmdline;
- size_t idx = cmdline.rfind("/");
+ size_t idx = cmdline.rfind('/');
if (idx != std::string::npos) {
cmdline = cmdline.substr(idx + 1);
}
@@ -257,9 +257,10 @@
}
struct PassthroughServiceManager : IServiceManager1_1 {
- static void openLibs(const std::string& fqName,
- std::function<bool /* continue */(void* /* handle */,
- const std::string& /* lib */, const std::string& /* sym */)> eachLib) {
+ static void openLibs(
+ const std::string& fqName,
+ const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
+ const std::string& /* sym */)>& eachLib) {
//fqName looks like android.hardware.foo@1.0::IFoo
size_t idx = fqName.find("::");