Don't allocate/construct additional std::string 66/74166/3
authorKyungwook Tak <k.tak@samsung.com>
Mon, 13 Jun 2016 05:03:05 +0000 (14:03 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 13 Jun 2016 05:11:30 +0000 (14:11 +0900)
file visitor is one of the most frequently running part of program so it
needed to being downsized is highly recommended

Change-Id: Ib317b9420329b1ad25b4db6310d3cce53b39f95f
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/service/file-system.cpp

index 6e3c94e..8b29beb 100644 (file)
@@ -93,11 +93,7 @@ bool File::isInApp(const std::string &path)
                else
                        continue;
 
-               auto types = File::getPkgTypes(pkgId);
-               if (types & static_cast<int>(Type::Package))
-                       return true;
-               else
-                       break;
+               return File::getPkgTypes(pkgId) != 0;
        }
 
        return false;
@@ -124,11 +120,7 @@ std::string File::getPkgPath(const std::string &path)
                        continue;
                }
 
-               auto types = File::getPkgTypes(pkgId);
-               if (types & static_cast<int>(Type::Package))
-                       return pkgPath;
-               else
-                       break;
+               return File::getPkgTypes(pkgId) != 0 ? pkgPath : path;
        }
 
        return path;
@@ -304,31 +296,38 @@ FilePtr FsVisitor::next()
                }
 
                auto &dir = this->m_dirs.front();
-               std::string filepath(result->d_name);
+               const auto &name = result->d_name;
+               auto name_size = ::strlen(name);
+
+               if (name_size == 0)
+                       continue;
 
                if (result->d_type == DT_DIR) {
-                       if (filepath.compare(".") != 0 && filepath.compare("..") != 0)
-                               this->m_dirs.emplace(
-                                       dir + ((filepath.back() == '/') ? filepath : (filepath + '/')));
+                       if (name_size == 1 && ::strcmp(name, ".") == 0)
+                               continue;
+                       else if (name_size == 2 && ::strcmp(name, "..") == 0)
+                               continue;
+
+                       this->m_dirs.emplace(dir + name + '/');
                } else if (result->d_type == DT_REG) {
                        try {
-                               auto fileptr = File::createIfModified(dir + filepath, this->m_since);
+                               auto fileptr = File::createIfModified(dir + name, this->m_since);
 
                                if (fileptr)
                                        return fileptr;
                        } catch (const Exception &e) {
                                if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST)
-                                       WARN("file not exist: " << dir << filepath << " msg: " << e.what());
+                                       WARN("file not exist: " << dir << name << " msg: " << e.what());
                                else if (e.error() == CSR_ERROR_FILE_SYSTEM)
                                        WARN("file type is not regular...? can it be happened?"
-                                                " :" << dir << filepath << " msg: " << e.what());
+                                                " :" << dir << name << " msg: " << e.what());
                                else
                                        throw;
                        }
                }
        }
 
-       ThrowExc(CSR_ERROR_FILE_SYSTEM, "readdir_r error on dir: " << this->m_dirs.front());
+       return nullptr;
 }
 
 } // namespace Csr