Improve performance 24/258924/10
authorChanggyu Choi <changyu.choi@samsung.com>
Fri, 28 May 2021 01:59:50 +0000 (10:59 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Mon, 31 May 2021 00:40:42 +0000 (09:40 +0900)
Changes:
 - Fix some unnecessary copy.
 - Add ReadyChecker for booting time.

Change-Id: I8454ff5ce28946f45375451a5ee2264a3f914024
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
26 files changed:
src/client/pkginfo_client.cc
src/common/database/abstract_db_handler.cc
src/common/database/abstract_db_handler.hh
src/common/database/appinfo_db_handler.cc
src/common/database/depinfo_db_handler.cc
src/common/ready_checker.cc [new file with mode: 0644]
src/common/ready_checker.hh [new file with mode: 0644]
src/common/request_handler/abstract_request_handler.hh
src/common/request_handler/command_request_handler.cc
src/common/request_handler/command_request_handler.hh
src/common/request_handler/get_appinfo_request_handler.cc
src/common/request_handler/get_appinfo_request_handler.hh
src/common/request_handler/get_cert_request_handler.cc
src/common/request_handler/get_cert_request_handler.hh
src/common/request_handler/get_depinfo_request_handler.cc
src/common/request_handler/get_depinfo_request_handler.hh
src/common/request_handler/get_pkginfo_request_handler.cc
src/common/request_handler/get_pkginfo_request_handler.hh
src/common/request_handler/query_request_handler.cc
src/common/request_handler/query_request_handler.hh
src/common/request_handler/set_cert_request_handler.cc
src/common/request_handler/set_cert_request_handler.hh
src/common/request_handler/set_pkginfo_request_handler.cc
src/common/request_handler/set_pkginfo_request_handler.hh
src/logging.hh
src/manager/pkginfo_manager.cc

index e877a66dc63a50480c908544bb77740cfaf52ee9..22c1cc0766a8b6821b6b9b7e3e2ba5c4f417267e 100644 (file)
@@ -18,7 +18,7 @@
 #include "set_cert_request_handler.hh"
 #include "set_pkginfo_request_handler.hh"
 #include "system_locale.hh"
-
+#include "ready_checker.hh"
 #include "pkgmgrinfo_debug.h"
 
 namespace pkgmgr_client {
@@ -38,12 +38,13 @@ PkgInfoClient::PkgInfoClient(
 }
 
 bool PkgInfoClient::SendRequest() {
+  static pkgmgr_common::ReadyChecker check_server(SERVER_READY);
   if (socket_ == nullptr) {
     LOGE("Socket is not ready");
     return false;
   }
 
-  if (access(SERVER_READY, F_OK) != 0) {
+  if (!check_server.IsReady()) {
     LOGW("Server is not ready, try to direct access");
     is_offline_ = true;
     return RequestHandlerDirectAccess();
index 80c1546c2b767cbccd10e0653e51f01923311c95..11e89e0c2c138a591c7f09591bf063804c7566bb 100644 (file)
@@ -231,7 +231,7 @@ AbstractDBHandler::AbstractDBHandler(uid_t uid, int pid) {
 }
 
 AbstractDBHandler::~AbstractDBHandler() {
-  for (auto db_handle : db_handle_list_)
+  for (auto& db_handle : db_handle_list_)
     sqlite3_close_v2(db_handle.first);
 }
 
@@ -256,7 +256,7 @@ bool AbstractDBHandler::Connect() {
   auto dbpath_list = GetDBPath();
   int ret = 0;
   sqlite3* db;
-  for (auto dbpath : dbpath_list) {
+  for (auto& dbpath : dbpath_list) {
     if (op_type_ == OPERATION_TYPE_READ) {
       ret = __open_read_db(dbpath.first.c_str(), &db, SQLITE_OPEN_READONLY |
           SQLITE_OPEN_URI);
@@ -284,12 +284,12 @@ void AbstractDBHandler::SetOpType(OperationType type) {
   op_type_ = type;
 }
 
-std::string AbstractDBHandler::GetLocale() { return locale_; }
+const std::string& AbstractDBHandler::GetLocale() { return locale_; }
 
 int AbstractDBHandler::GetPID() { return pid_; }
 
-void AbstractDBHandler::SetLocale(const std::string& locale) {
-  locale_ = locale;
+void AbstractDBHandler::SetLocale(std::string locale) {
+  locale_ = std::move(locale);
 }
 
 void AbstractDBHandler::SetDBType(DBType type) { db_type_ = type; }
index 8590405ab89de7a640e6ddd5b1e520ba1f91baf3..ec4cd6c0665f9a8c17620221722a8c9a80b4e509 100644 (file)
@@ -48,7 +48,7 @@ class EXPORT_API AbstractDBHandler {
   AbstractDBHandler(uid_t uid, pid_t pid);
   virtual ~AbstractDBHandler();
   virtual int Execute() = 0;
-  void SetLocale(const std::string& locale);
+  void SetLocale(std::string locale);
   void SetDBType(DBType type);
   void SetOpType(OperationType type);
   OperationType GetOpType();
@@ -57,7 +57,7 @@ class EXPORT_API AbstractDBHandler {
   bool Connect();
   int GetPID();
   std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
-  std::string GetLocale();
+  const std::string& GetLocale();
   static std::shared_timed_mutex lock_;
  private:
   std::vector<std::pair<std::string, uid_t>> GetDBPath();
index ee23cc805dffd953b808affda7bbcaf3a9e6cfd6..668de72c8aa89b166097d2d7d2c238c018ed2154 100644 (file)
@@ -66,7 +66,7 @@ int AppInfoDBHandler::Execute() {
       NULL, __free_applications);
   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
   int ret = PMINFO_R_OK;
-  for (auto conn : conn_list) {
+  for (auto& conn : conn_list) {
     ret = appinfo_internal_filter_get_list(conn.first, filter_, conn.second,
         uid_, GetLocale().c_str(), list);
     if (ret == PMINFO_R_ERROR) {
index c0322567b42f0b6ab1af8137cf869c046b8ea937..7660470e381978091b2e29a55a0d513ec63bd708 100644 (file)
@@ -47,7 +47,7 @@ int DepInfoGetDBHandler::Execute() {
     _LOGE("Failed to connect database");
     return PMINFO_R_ERROR;
   }
-  GList *list = nullptr;
+  GListlist = nullptr;
   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
   int ret = PMINFO_R_OK;
   for (auto& conn : conn_list) {
@@ -57,7 +57,7 @@ int DepInfoGetDBHandler::Execute() {
       break;
   }
 
-  for (GList *tmp = list; tmp; tmp = tmp->next)
+  for (GListtmp = list; tmp; tmp = tmp->next)
     dependency_list_.emplace_back(reinterpret_cast<dependency_x *>(tmp->data));
 
   g_list_free(list);
diff --git a/src/common/ready_checker.cc b/src/common/ready_checker.cc
new file mode 100644 (file)
index 0000000..48e4c5c
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ready_checker.hh"
+
+#include <utility>
+
+#include "pkgmgrinfo_debug.h"
+
+#undef LOG_TAG
+#define LOG_TAG "PKGMGR_INFO"
+
+namespace pkgmgr_common {
+
+gboolean ReadyChecker::OnReceiveEvent(GIOChannel* channel, GIOCondition cond,
+    gpointer user_data) {
+  char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event))));
+  char* ptr;
+  ssize_t len;
+  struct inotify_event* event;
+  auto ready_checker = reinterpret_cast<ReadyChecker*>(user_data);
+  int fd = g_io_channel_unix_get_fd(channel);
+  while ((len = read(fd, buf, sizeof(buf))) > 0) {
+    for (ptr = buf; ptr < buf + len;
+         ptr += sizeof(struct inotify_event) + event->len) {
+      event = reinterpret_cast<struct inotify_event*>(ptr);
+      char* nptr = ptr + sizeof(struct inotify_event) + event->len;
+      if (nptr > buf + len)
+        break;
+
+      if (event->len) {
+        if (event->mask & IN_CREATE &&
+            ready_checker->ready_file_ == event->name) {
+          _LOGI("server is ready (%s)", ready_checker->ready_file_.c_str());
+          ready_checker->ready_ = true;
+          ready_checker->Dispose();
+          return G_SOURCE_CONTINUE;
+        }
+      }
+    }
+  }
+
+  return G_SOURCE_CONTINUE;
+}
+
+ReadyChecker::ReadyChecker(const std::string& ready_path) {
+  if (access(ready_path.c_str(), F_OK) == 0) {
+    ready_ = true;
+    disposed_ = true;
+    return;
+  }
+  auto it = ready_path.find_last_of("/");
+  if (it == ready_path.npos) {
+    _LOGE("Invalid path %s", ready_path.c_str());
+    disposed_ = true;
+    return;
+  }
+
+  fd_ = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
+  if (fd_ == -1) {
+    _LOGE("Failed to inotify_init. errno(%d)", errno);
+    disposed_ = true;
+    return;
+  }
+  ready_path_ = ready_path.substr(0, it);
+  ready_file_ = ready_path.substr(it + 1);
+  wd_ = inotify_add_watch(fd_, ready_path_.c_str(), IN_CREATE);
+  if (wd_ == -1) {
+    _LOGE("Failed to inotify_add_watch. errno(%d)", errno);
+    Dispose();
+    return;
+  }
+
+  channel_ = g_io_channel_unix_new(fd_);
+  if (channel_ == nullptr) {
+    _LOGE("Failed to create GIO channel");
+    Dispose();
+    return;
+  }
+  tag_ = g_io_add_watch(channel_, G_IO_IN, OnReceiveEvent, this);
+  if (tag_ == 0) {
+    _LOGE("Failed to add watch");
+    Dispose();
+    return;
+  }
+
+  disposed_ = false;
+}
+
+ReadyChecker::~ReadyChecker() {
+  Dispose();
+}
+
+bool ReadyChecker::IsReady() const {
+  return ready_;
+}
+
+void ReadyChecker::Dispose() {
+  if (disposed_)
+    return;
+
+  if (tag_) {
+    g_source_remove(tag_);
+    tag_ = 0;
+  }
+
+  if (channel_ != nullptr) {
+    g_io_channel_unref(channel_);
+    channel_ = nullptr;
+  }
+
+  if (wd_ > 0) {
+    inotify_rm_watch(fd_, wd_);
+    wd_ = 0;
+  }
+
+  if (fd_ > 0) {
+    close(fd_);
+    fd_ = 0;
+  }
+
+  disposed_ = true;
+}
+
+}  // namespace pkgmgr_common
diff --git a/src/common/ready_checker.hh b/src/common/ready_checker.hh
new file mode 100644 (file)
index 0000000..bd6a2a0
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gio/gio.h>
+#include <glib.h>
+#include <sys/inotify.h>
+
+#include <string>
+
+#ifndef READY_CHECKER_HH_
+#define READY_CHECKER_HH_
+
+namespace pkgmgr_common {
+
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__((visibility("default")))
+#endif
+
+class EXPORT_API ReadyChecker {
+ public:
+  explicit ReadyChecker(const std::string& ready_path);
+  ~ReadyChecker();
+  ReadyChecker(const ReadyChecker&) = delete;
+  ReadyChecker& operator = (const ReadyChecker&) = delete;
+  bool IsReady() const;
+
+ private:
+  void Dispose();
+  static gboolean OnReceiveEvent(GIOChannel* channel, GIOCondition cond,
+      gpointer user_data);
+
+ private:
+  std::string ready_path_;
+  std::string ready_file_;
+  int fd_ = 0;
+  int wd_ = 0;
+  GIOChannel* channel_ = nullptr;
+  int tag_ = 0;
+  bool ready_ = false;
+  bool disposed_ = false;
+};
+
+}  // namespace pkgmgr_common
+
+#endif  // READY_CHECKER_HH_
index cda2c99c74fe2712101797cc998ea8bec03d755b..8285e80bdc4d0bebb0f84decb7c4b3a1831c955e 100644 (file)
@@ -21,7 +21,8 @@ namespace request_handler {
 class EXPORT_API AbstractRequestHandler {
  public:
   virtual ~AbstractRequestHandler() = default;
-  virtual bool HandleRequest(unsigned char* data, int size, std::string locale) = 0;
+  virtual bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) = 0;
 
   virtual std::vector<uint8_t> ExtractResult() = 0;
 
index b48a64a3d0647b52dcff718a79b1f1efa16cb0f0..f10fcd110dea64e2190f4afd9ee1890f674a14df 100644 (file)
@@ -19,7 +19,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool CommandRequestHandler::HandleRequest(unsigned char* data, int size,
-                                             std::string locale) {
+                                             const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index 04d7276c4b5a3eeb357bb429aeb41d0747597ed7..aa4cb4b3d757188fde231c82588bf3a81d03ac85 100644 (file)
@@ -20,7 +20,8 @@ namespace request_handler {
 
 class EXPORT_API CommandRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index db17565e8ed465ee3e5faec8b699e9248e767a2b..f4428eeb2739416576c4153c0ae98beea5df62b1 100644 (file)
@@ -19,7 +19,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool GetAppinfoRequestHandler::HandleRequest(unsigned char* data, int size,
-                                             std::string locale) {
+                                             const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index 56ea25e5348b5b131351d53c4ebbd3b401b7c4fd..8fac2d9d1a32e06fe226ba1d7d0cb229424358d0 100644 (file)
@@ -19,7 +19,8 @@ namespace request_handler {
 
 class EXPORT_API GetAppinfoRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index 3857924bd27f4fc2504b6a3924bda7110b43511e..a42d2166099e6070e3b62408248f29a8f866291b 100644 (file)
@@ -18,7 +18,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool GetCertRequestHandler::HandleRequest(unsigned char* data, int size,
-                                          std::string locale) {
+                                          const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index 45a95c4697e67194745fe92db4bc423be4ebffa6..4f9d9af8570394c815d21d2d59a10e273834c57f 100644 (file)
@@ -19,7 +19,8 @@ namespace request_handler {
 
 class EXPORT_API GetCertRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index d820b72b261be997d353a821bd0400f5de60821d..6c1a26cb5c9a75c0b7f933ba85300038b3f5720e 100644 (file)
@@ -19,7 +19,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool GetDepinfoRequestHandler::HandleRequest(unsigned char* data, int size,
-                                             std::string locale) {
+                                             const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index d0c9603e487aac9c754798d2d754e4ec68de26f0..44bb7a9ce3fddaf86370cc679ccf47a1f32a4d46 100644 (file)
@@ -19,7 +19,8 @@ namespace request_handler {
 
 class EXPORT_API GetDepinfoRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index cdd166ccb9153b42a2f0a16b2761b8adbd005458..1630ea39b7064d384b2aca52fb1f0001b884b4e4 100644 (file)
@@ -20,7 +20,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool GetPkginfoRequestHandler::HandleRequest(unsigned char* data, int size,
-                                             std::string locale) {
+                                             const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index 037b0f8c45ab420d7d349d9b12547ff91cb03890..0bf022f1dfa52f47a44f53843f5e5c0abf05cabb 100644 (file)
@@ -19,7 +19,8 @@ namespace request_handler {
 
 class EXPORT_API GetPkginfoRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index ea1a73c638c80ab5ea8ec4b2be77d1f39adca6be..7e5769a70a0f6445d79afd1c3ed9211db4b7d191 100644 (file)
@@ -19,7 +19,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool QueryRequestHandler::HandleRequest(unsigned char* data, int size,
-                                        std::string locale) {
+                                        const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index 6799f84782e04f5031b2efe69b1e6992dd06b629..7d6d26790707ddde26f9d50107711c8ca75f5223 100644 (file)
@@ -20,7 +20,8 @@ namespace request_handler {
 
 class EXPORT_API QueryRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index 9ac455e9f0633b771b6d2622b57c23959cde39a5..82fe6e492d1a435b0fe3a8a9dec47e0747ad487f 100644 (file)
@@ -19,7 +19,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool SetCertRequestHandler::HandleRequest(unsigned char* data, int size,
-                                          std::string locale) {
+                                          const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index 477852e4eec568bb310dcdb33694e72baaad15b8..dae2054c890ecabc9d96ff30fcdda90a96dcb25b 100644 (file)
@@ -19,7 +19,8 @@ namespace request_handler {
 
 class EXPORT_API SetCertRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index 50837e451fab9e13a859f1fd3855cf72eed495b1..cf04ccfa64c0c36bc1846a6635db8bfe491cd54e 100644 (file)
@@ -21,7 +21,7 @@ namespace pkgmgr_server {
 namespace request_handler {
 
 bool SetPkginfoRequestHandler::HandleRequest(unsigned char* data, int size,
-                                             std::string locale) {
+                                             const std::string& locale) {
   auto abstract_parcel =
       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
 
index 7fb1efde87d796b5ead49410652e584bb2748085..8006920127973ee8dbe233f68b3c2326e31c9512 100644 (file)
@@ -19,7 +19,8 @@ namespace request_handler {
 
 class EXPORT_API SetPkginfoRequestHandler : public AbstractRequestHandler {
  public:
-  bool HandleRequest(unsigned char* data, int size, std::string locale) override;
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
 
   std::vector<uint8_t> ExtractResult() override;
 
index ddce05a2ec0ab5b44348e9529e01787755c9cfa3..318c2d82f8a71fed6dd7803af2271691a0d49fb6 100644 (file)
@@ -115,7 +115,7 @@ class LogCore {
   }
 
   void Log(LogLevel level, const std::string& tag, const std::string& log) {
-    for (auto backend : backend_list_)
+    for (auto& backend : backend_list_)
       backend->WriteLog(level, tag, log);
   }
 
index e10d19d951068e8a401c076b3eac020b56dca19d..218f29c88380f2f501e24e30f4fbffd3e63b96b5 100644 (file)
@@ -24,6 +24,7 @@
 #include <parcel.hh>
 
 #include <map>
+#include <memory>
 #include <string>
 #include <utility>
 #include <vector>
@@ -96,7 +97,7 @@ extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid,
     LOG(DEBUG) << "No packages meets given condition for user " << uid;
     return PMINFO_R_ENOENT;
   }
-  for (auto &pkginfo : result_list)
+  for (auto pkginfo : result_list)
     g_hash_table_insert(packages, (gpointer)pkginfo->package,
                         (gpointer)pkginfo);
 
@@ -133,7 +134,7 @@ extern "C" EXPORT_API int _pkginfo_get_depends_on(uid_t uid,
       std::static_pointer_cast<pcp::DepInfoParcelable>(ptr));
 
   auto dependency_list = return_parcel->ExtractDependencyInfo();
-  for (auto &dependency : dependency_list)
+  for (auto dependency : dependency_list)
     *dependencies = g_list_prepend(*dependencies, dependency);
   return PMINFO_R_OK;
 }
@@ -214,10 +215,10 @@ extern "C" EXPORT_API char* _appinfo_get_localed_label(
   // result_list is vector of string vector
   char* label = nullptr;
   auto result_list = return_parcel->GetResult();
-  for (auto result : result_list) {
+  for (auto& result : result_list) {
     // result is string vector
     // it only has one string or not.
-    if (result.front().empty() || result.front().length() == 0)
+    if (result.front().empty())
       return nullptr;
     label = strdup(result.front().c_str());
     if (label == nullptr) {
@@ -266,11 +267,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info(
   auto result_list = return_parcel->GetResult();
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
-  for (auto result : result_list) {
-    if (result.size() != 2)
-      return PMINFO_R_ERROR;
-    if (result.front().empty() || result.front().size() == 0 ||
-        result.back().empty() || result.back().size() == 0)
+  for (auto& result : result_list) {
+    if (result.size() != 2 || result.front().empty() || result.back().empty())
       return PMINFO_R_ERROR;
     *appid = strdup(result.front().c_str());
     *access = strdup(result.back().c_str());
@@ -319,10 +317,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_appid(
   auto result_list = return_parcel->GetResult();
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
-  for (auto result : result_list) {
-    if (result.size() != 1)
-      return PMINFO_R_ERROR;
-    if (result.front().empty() || result.front().size() == 0)
+  for (auto& result : result_list) {
+    if (result.size() != 1 || result.front().empty())
       return PMINFO_R_ERROR;
     *appid = strdup(result.front().c_str());
     if (*appid == nullptr) {
@@ -372,11 +368,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info(
   auto result_list = return_parcel->GetResult();
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
-  for (auto result : result_list) {
-    if (result.size() != 2)
-      return PMINFO_R_ERROR;
-    if (result.front().empty() || result.front().size() == 0 ||
-        result.back().empty() || result.back().size() == 0)
+  for (auto& result : result_list) {
+    if (result.size() != 2 || result.front().empty() || result.back().empty())
       return PMINFO_R_ERROR;
     *appid = strdup(result.front().c_str());
     *trusted = strdup(result.back().c_str());
@@ -427,10 +420,8 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges(
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
 
-  for (auto result : result_list) {
-    if (result.size() != 1)
-      return PMINFO_R_ERROR;
-    if (result.front().empty() || result.front().size() == 0)
+  for (auto& result : result_list) {
+    if (result.size() != 1 || result.front().empty())
       return PMINFO_R_ERROR;
     char* privilege = strdup(result.front().c_str());
     if (privilege == nullptr) {
@@ -480,14 +471,10 @@ extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges(
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
 
-  for (auto result : result_list) {
-    if (result.size() != 2)
-      return PMINFO_R_ERROR;
-    if (result.front().empty() || result.front().size() == 0 ||
-        result.back().empty() || result.back().size() == 0)
+  for (auto& result : result_list) {
+    if (result.size() != 2 || result.front().empty() || result.back().empty())
       return PMINFO_R_ERROR;
-    std::string app_control = result.front();
-    std::stringstream ss(app_control);
+    std::stringstream ss(result.front());
     std::string token;
     while (std::getline(ss, token, '|')) {
       if (token.compare(std::string(operation))) {
@@ -551,7 +538,7 @@ extern "C" EXPORT_API int _plugininfo_get_appids(
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
 
-  for (auto result : result_list) {
+  for (auto& result : result_list) {
     if (result.size() != 1) {
       LOG(ERROR) << "Invalid result";
       g_list_free_full(*list, free);
@@ -643,7 +630,7 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid,
   if (result_list.size() == 0)
     return PMINFO_R_ENOENT;
 
-  for (auto result : result_list) {
+  for (auto& result : result_list) {
     if (result.size() != 3) {
       LOG(ERROR) << "Invalid result";
       g_slist_free_full(*update_info_list, __free_update_info);
@@ -763,18 +750,18 @@ extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char* l_pkgid,
   result_map.insert(make_pair(std::string(l_pkgid), "-1"));
   result_map.insert(make_pair(std::string(r_pkgid), "-1"));
 
-  for (auto &certinfo : certinfo_list)
+  for (autocertinfo : certinfo_list)
     result_map[certinfo.front()] = certinfo.back();
 
-  if (result_map.find(std::string(l_pkgid))->second == "-1" &&
-      result_map.find(std::string(r_pkgid))->second == "-1")
+  auto l_iter = result_map.find(l_pkgid);
+  auto r_iter = result_map.find(r_pkgid);
+  if (l_iter->second == "-1" && r_iter->second == "-1")
     *result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
-  else if (result_map.find(std::string(l_pkgid))->second == "-1")
+  else if (l_iter->second == "-1")
     *result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
-  else if (result_map.find(std::string(r_pkgid))->second == "-1")
+  else if (r_iter->second == "-1")
     *result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
-  else if (result_map.find(std::string(l_pkgid))->second ==
-      result_map.find(std::string(r_pkgid))->second)
+  else if (l_iter->second == r_iter->second)
     *result = PMINFO_CERT_COMPARE_MATCH;
   else
     *result = PMINFO_CERT_COMPARE_MISMATCH;
@@ -816,21 +803,24 @@ extern "C" EXPORT_API int _certinfo_compare_app_certinfo(uid_t uid,
 
   auto pkgid_list = return_parcel->GetResult();
   std::map<std::string, std::string> result_map;
-  for (auto &pkgid : pkgid_list)
+  for (autopkgid : pkgid_list)
     result_map.insert(make_pair(pkgid.front(), pkgid.back()));
 
-  if (result_map.find(std::string(l_appid)) == result_map.end()) {
+  auto l_iter = result_map.find(l_appid);
+  if (l_iter == result_map.end()) {
     LOG(ERROR) << "Cannot find pkgid of app " << l_appid
         << " for uid " << uid;
     return PMINFO_R_ENOENT;
-  } else if (result_map.find(std::string(r_appid)) == result_map.end()) {
+  }
+  auto r_iter = result_map.find(r_appid);
+  if (r_iter == result_map.end()) {
     LOG(ERROR) << "Cannot find pkgid of app " << r_appid
         << " for uid " << uid;
     return PMINFO_R_ENOENT;
   }
 
-  const char* l_pkgid = result_map.find(std::string(l_appid))->second.c_str();
-  const char* r_pkgid = result_map.find(std::string(r_appid))->second.c_str();
+  const char* l_pkgid = l_iter->second.c_str();
+  const char* r_pkgid = r_iter->second.c_str();
 
   return _certinfo_compare_pkg_certinfo(l_pkgid, r_pkgid, result);
 }
@@ -1131,17 +1121,17 @@ extern "C" EXPORT_API int _parser_clear_cache_memory_db(uid_t uid) {
     return PMINFO_R_ERROR;
 
   auto ptr = client.GetResultParcel();
-  if (ptr == nullptr)   {
+  if (ptr == nullptr) {
     LOG(ERROR) << "Fail to get return parcelable";
     return PMINFO_R_ERROR;
   }
 
-  if (ptr->GetRequestResult() != PMINFO_R_OK)   {
+  if (ptr->GetRequestResult() != PMINFO_R_OK) {
     LOG(ERROR) << "Request fail";
     return PMINFO_R_ERROR;
   }
 
-  if (ptr->GetType() != pcp::ParcelableType::Result)   {
+  if (ptr->GetType() != pcp::ParcelableType::Result) {
     LOG(ERROR) << "Invalid parcelable type";
     return PMINFO_R_ERROR;
   }