From 1a9d736f28f176c27055d64881aae67883e4e28f Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Thu, 21 Dec 2017 13:35:41 +0100 Subject: [PATCH] src: improved cache implementation * use const ref in cache * improved performance of FilePath caching in FileRegister --- src/lib/CMakeLists.txt | 3 +- src/lib/data/storage/PersistentStorage.cpp | 5 ++- src/lib/utility/OrderedCache.h | 45 +++++++++++++++++++ src/lib/utility/{Cache.h => UnorderedCache.h} | 20 ++++----- src/lib/utility/file/FileRegister.cpp | 6 +-- src/lib/utility/file/FileRegister.h | 4 +- src/lib_cxx/data/parser/cxx/CxxContext.h | 6 +-- 7 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/lib/utility/OrderedCache.h rename src/lib/utility/{Cache.h => UnorderedCache.h} (53%) diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index b5c89aa2..a2503cc2 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -510,9 +510,9 @@ add_files( utility/ApplicationArchitectureType.h utility/AppPath.cpp utility/AppPath.h - utility/Cache.h utility/ConfigManager.cpp utility/ConfigManager.h + utility/OrderedCache.h utility/OsType.h utility/Property.h utility/ResourcePaths.cpp @@ -526,6 +526,7 @@ add_files( utility/tracing.h utility/Tree.h utility/types.h + utility/UnorderedCache.h utility/UserPaths.cpp utility/UserPaths.h utility/utility.cpp diff --git a/src/lib/data/storage/PersistentStorage.cpp b/src/lib/data/storage/PersistentStorage.cpp index 6791e8ed..372417c8 100644 --- a/src/lib/data/storage/PersistentStorage.cpp +++ b/src/lib/data/storage/PersistentStorage.cpp @@ -14,7 +14,7 @@ #include "data/parser/ParseLocation.h" #include "data/NodeTypeSet.h" #include "settings/ApplicationSettings.h" -#include "utility/Cache.h" +#include "utility/UnorderedCache.h" #include "utility/file/FileInfo.h" #include "utility/file/FilePath.h" #include "utility/logging/logging.h" @@ -1624,7 +1624,8 @@ std::vector PersistentStorage::getAllEdgeBookmarks() const std::vector edgeBookmarks; - Cache nodeIdCache([&](std::string serializedNodeName) + UnorderedCache nodeIdCache( + [&](const std::string& serializedNodeName) { return m_sqliteIndexStorage.getNodeBySerializedName(serializedNodeName).id; } diff --git a/src/lib/utility/OrderedCache.h b/src/lib/utility/OrderedCache.h new file mode 100644 index 00000000..f0ef24e5 --- /dev/null +++ b/src/lib/utility/OrderedCache.h @@ -0,0 +1,45 @@ +#ifndef ORDERED_CACHE_H +#define ORDERED_CACHE_H + +#include +#include + +template +class OrderedCache +{ +public: + OrderedCache(std::function calculator); + ValType getValue(const KeyType& key); + +private: + std::function m_calculator; + std::map m_map; + + size_t m_hitCount; + size_t m_missCount; +}; + +template +OrderedCache::OrderedCache(std::function calculator) + : m_calculator(calculator) + , m_hitCount(0) + , m_missCount(0) +{ +} + +template +ValType OrderedCache::getValue(const KeyType& key) +{ + typename std::map::const_iterator it = m_map.find(key); + if (it != m_map.end()) + { + ++m_hitCount; + return it->second; + } + ++m_missCount; + ValType val = m_calculator(key); + m_map.insert(std::pair(key, val)); + return val; +} + +#endif // ORDERED_CACHE_H diff --git a/src/lib/utility/Cache.h b/src/lib/utility/UnorderedCache.h similarity index 53% rename from src/lib/utility/Cache.h rename to src/lib/utility/UnorderedCache.h index 47ee4649..4ae043fa 100644 --- a/src/lib/utility/Cache.h +++ b/src/lib/utility/UnorderedCache.h @@ -1,18 +1,18 @@ -#ifndef CACHE_H -#define CACHE_H +#ifndef UNORDERED_CACHE_H +#define UNORDERED_CACHE_H #include #include template > -class Cache +class UnorderedCache { public: - Cache(std::function calculator); - ValType getValue(KeyType key); + UnorderedCache(std::function calculator); + ValType getValue(const KeyType& key); private: - std::function m_calculator; + std::function m_calculator; std::unordered_map m_map; size_t m_hitCount; @@ -20,7 +20,7 @@ private: }; template -Cache::Cache(std::function calculator) +UnorderedCache::UnorderedCache(std::function calculator) : m_calculator(calculator) , m_hitCount(0) , m_missCount(0) @@ -28,9 +28,9 @@ Cache::Cache(std::function calculato } template -ValType Cache::getValue(KeyType key) +ValType UnorderedCache::getValue(const KeyType& key) { - typename std::unordered_map::const_iterator it = m_map.find(key); + typename std::unordered_map::const_iterator it = m_map.find(key); if (it != m_map.end()) { ++m_hitCount; @@ -42,4 +42,4 @@ ValType Cache::getValue(KeyType key) return val; } -#endif // CACHE_H +#endif // UNORDERED_CACHE_H diff --git a/src/lib/utility/file/FileRegister.cpp b/src/lib/utility/file/FileRegister.cpp index 7979579a..d7576b50 100644 --- a/src/lib/utility/file/FileRegister.cpp +++ b/src/lib/utility/file/FileRegister.cpp @@ -13,8 +13,8 @@ FileRegister::FileRegister( , m_indexedPaths(indexedPaths) , m_excludedPaths(excludedPaths) , m_hasFilePathCache( - [&](std::string f){ - const FilePath filePath(f); + [&](const FilePath& filePath) + { bool ret = false; if (filePath == m_currentPath) @@ -100,5 +100,5 @@ bool FileRegister::fileIsIndexed(const FilePath& filePath) const bool FileRegister::hasFilePath(const FilePath& filePath) const { - return m_hasFilePathCache.getValue(filePath.str()); + return m_hasFilePathCache.getValue(filePath); } diff --git a/src/lib/utility/file/FileRegister.h b/src/lib/utility/file/FileRegister.h index 13ce5548..febf111e 100644 --- a/src/lib/utility/file/FileRegister.h +++ b/src/lib/utility/file/FileRegister.h @@ -4,7 +4,7 @@ #include #include "utility/file/FileRegisterStateData.h" -#include "utility/Cache.h" +#include "utility/OrderedCache.h" class FileRegister { @@ -29,7 +29,7 @@ private: const FilePath& m_currentPath; const std::set m_indexedPaths; const std::set m_excludedPaths; - mutable Cache m_hasFilePathCache; + mutable OrderedCache m_hasFilePathCache; }; #endif // FILE_REGISTER_H diff --git a/src/lib_cxx/data/parser/cxx/CxxContext.h b/src/lib_cxx/data/parser/cxx/CxxContext.h index 6349f276..370705ad 100644 --- a/src/lib_cxx/data/parser/cxx/CxxContext.h +++ b/src/lib_cxx/data/parser/cxx/CxxContext.h @@ -4,10 +4,10 @@ #include #include "data/name/NameHierarchy.h" -#include "utility/Cache.h" +#include "utility/UnorderedCache.h" -typedef Cache DeclNameCache; -typedef Cache TypeNameCache; +typedef UnorderedCache DeclNameCache; +typedef UnorderedCache TypeNameCache; class CxxContext {