From ee8d35f084acba634177866cedf2722cb2642224 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Thu, 23 Apr 2015 22:31:51 +0200 Subject: [PATCH] utility: Fixed bad performance of FilePath comparison --- src/lib/utility/file/FilePath.cpp | 20 +++++++++++++++----- src/lib/utility/file/FilePath.h | 3 +++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib/utility/file/FilePath.cpp b/src/lib/utility/file/FilePath.cpp index 1cfc9354..324fcf72 100644 --- a/src/lib/utility/file/FilePath.cpp +++ b/src/lib/utility/file/FilePath.cpp @@ -2,22 +2,28 @@ FilePath::FilePath(const char* filePath) : m_path(filePath) + , m_exists(false) { + init(); } FilePath::FilePath(const std::string& filePath) : m_path(filePath) + , m_exists(false) { + init(); } FilePath::FilePath(const boost::filesystem::path& filePath) : m_path(filePath) + , m_exists(false) { + init(); } bool FilePath::exists() const { - return boost::filesystem::exists(m_path); + return m_exists; } std::string FilePath::str() const @@ -75,10 +81,14 @@ bool FilePath::operator!=(const FilePath& other) const bool FilePath::operator<(const FilePath& other) const { - if (exists() && other.exists()) + return m_path.compare(other.m_path) < 0; +} + +void FilePath::init() +{ + if (boost::filesystem::exists(m_path)) { - return boost::filesystem::canonical(m_path).compare(boost::filesystem::canonical(other.m_path)) < 0; + m_exists = true; + m_path = boost::filesystem::canonical(m_path); } - - return boost::filesystem::absolute(m_path).compare(boost::filesystem::absolute(other.m_path)) < 0; } diff --git a/src/lib/utility/file/FilePath.h b/src/lib/utility/file/FilePath.h index 1a38737c..deea1388 100644 --- a/src/lib/utility/file/FilePath.h +++ b/src/lib/utility/file/FilePath.h @@ -27,7 +27,10 @@ public: bool operator<(const FilePath& other) const; private: + void init(); + boost::filesystem::path m_path; + bool m_exists; }; #endif // FILE_PATH_H