src: improved cache implementation

* use const ref in cache
* improved performance of FilePath caching in FileRegister
This commit is contained in:
mlangkabel
2017-12-21 13:35:41 +01:00
parent 88d90bde8f
commit 1a9d736f28
7 changed files with 68 additions and 21 deletions
+2 -1
View File
@@ -510,9 +510,9 @@ add_files(
utility/ApplicationArchitectureType.h utility/ApplicationArchitectureType.h
utility/AppPath.cpp utility/AppPath.cpp
utility/AppPath.h utility/AppPath.h
utility/Cache.h
utility/ConfigManager.cpp utility/ConfigManager.cpp
utility/ConfigManager.h utility/ConfigManager.h
utility/OrderedCache.h
utility/OsType.h utility/OsType.h
utility/Property.h utility/Property.h
utility/ResourcePaths.cpp utility/ResourcePaths.cpp
@@ -526,6 +526,7 @@ add_files(
utility/tracing.h utility/tracing.h
utility/Tree.h utility/Tree.h
utility/types.h utility/types.h
utility/UnorderedCache.h
utility/UserPaths.cpp utility/UserPaths.cpp
utility/UserPaths.h utility/UserPaths.h
utility/utility.cpp utility/utility.cpp
+3 -2
View File
@@ -14,7 +14,7 @@
#include "data/parser/ParseLocation.h" #include "data/parser/ParseLocation.h"
#include "data/NodeTypeSet.h" #include "data/NodeTypeSet.h"
#include "settings/ApplicationSettings.h" #include "settings/ApplicationSettings.h"
#include "utility/Cache.h" #include "utility/UnorderedCache.h"
#include "utility/file/FileInfo.h" #include "utility/file/FileInfo.h"
#include "utility/file/FilePath.h" #include "utility/file/FilePath.h"
#include "utility/logging/logging.h" #include "utility/logging/logging.h"
@@ -1624,7 +1624,8 @@ std::vector<EdgeBookmark> PersistentStorage::getAllEdgeBookmarks() const
std::vector<EdgeBookmark> edgeBookmarks; std::vector<EdgeBookmark> edgeBookmarks;
Cache<std::string, Id> nodeIdCache([&](std::string serializedNodeName) UnorderedCache<std::string, Id> nodeIdCache(
[&](const std::string& serializedNodeName)
{ {
return m_sqliteIndexStorage.getNodeBySerializedName(serializedNodeName).id; return m_sqliteIndexStorage.getNodeBySerializedName(serializedNodeName).id;
} }
+45
View File
@@ -0,0 +1,45 @@
#ifndef ORDERED_CACHE_H
#define ORDERED_CACHE_H
#include <functional>
#include <map>
template <typename KeyType, typename ValType>
class OrderedCache
{
public:
OrderedCache(std::function<ValType(const KeyType&)> calculator);
ValType getValue(const KeyType& key);
private:
std::function<ValType(const KeyType&)> m_calculator;
std::map<KeyType, ValType> m_map;
size_t m_hitCount;
size_t m_missCount;
};
template <typename KeyType, typename ValType>
OrderedCache<KeyType, ValType>::OrderedCache(std::function<ValType(const KeyType&)> calculator)
: m_calculator(calculator)
, m_hitCount(0)
, m_missCount(0)
{
}
template <typename KeyType, typename ValType>
ValType OrderedCache<KeyType, ValType>::getValue(const KeyType& key)
{
typename std::map<KeyType, ValType>::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<KeyType, ValType>(key, val));
return val;
}
#endif // ORDERED_CACHE_H
@@ -1,18 +1,18 @@
#ifndef CACHE_H #ifndef UNORDERED_CACHE_H
#define CACHE_H #define UNORDERED_CACHE_H
#include <functional> #include <functional>
#include <unordered_map> #include <unordered_map>
template <typename KeyType, typename ValType, typename Hasher = std::hash<KeyType>> template <typename KeyType, typename ValType, typename Hasher = std::hash<KeyType>>
class Cache class UnorderedCache
{ {
public: public:
Cache(std::function<ValType(KeyType)> calculator); UnorderedCache(std::function<ValType(const KeyType&)> calculator);
ValType getValue(KeyType key); ValType getValue(const KeyType& key);
private: private:
std::function<ValType(KeyType)> m_calculator; std::function<ValType(const KeyType&)> m_calculator;
std::unordered_map<KeyType, ValType, Hasher> m_map; std::unordered_map<KeyType, ValType, Hasher> m_map;
size_t m_hitCount; size_t m_hitCount;
@@ -20,7 +20,7 @@ private:
}; };
template <typename KeyType, typename ValType, typename Hasher> template <typename KeyType, typename ValType, typename Hasher>
Cache<KeyType, ValType, Hasher>::Cache(std::function<ValType(KeyType)> calculator) UnorderedCache<KeyType, ValType, Hasher>::UnorderedCache(std::function<ValType(const KeyType&)> calculator)
: m_calculator(calculator) : m_calculator(calculator)
, m_hitCount(0) , m_hitCount(0)
, m_missCount(0) , m_missCount(0)
@@ -28,9 +28,9 @@ Cache<KeyType, ValType, Hasher>::Cache(std::function<ValType(KeyType)> calculato
} }
template <typename KeyType, typename ValType, typename Hasher> template <typename KeyType, typename ValType, typename Hasher>
ValType Cache<KeyType, ValType, Hasher>::getValue(KeyType key) ValType UnorderedCache<KeyType, ValType, Hasher>::getValue(const KeyType& key)
{ {
typename std::unordered_map<KeyType, ValType>::const_iterator it = m_map.find(key); typename std::unordered_map<KeyType, ValType, Hasher>::const_iterator it = m_map.find(key);
if (it != m_map.end()) if (it != m_map.end())
{ {
++m_hitCount; ++m_hitCount;
@@ -42,4 +42,4 @@ ValType Cache<KeyType, ValType, Hasher>::getValue(KeyType key)
return val; return val;
} }
#endif // CACHE_H #endif // UNORDERED_CACHE_H
+3 -3
View File
@@ -13,8 +13,8 @@ FileRegister::FileRegister(
, m_indexedPaths(indexedPaths) , m_indexedPaths(indexedPaths)
, m_excludedPaths(excludedPaths) , m_excludedPaths(excludedPaths)
, m_hasFilePathCache( , m_hasFilePathCache(
[&](std::string f){ [&](const FilePath& filePath)
const FilePath filePath(f); {
bool ret = false; bool ret = false;
if (filePath == m_currentPath) if (filePath == m_currentPath)
@@ -100,5 +100,5 @@ bool FileRegister::fileIsIndexed(const FilePath& filePath) const
bool FileRegister::hasFilePath(const FilePath& filePath) const bool FileRegister::hasFilePath(const FilePath& filePath) const
{ {
return m_hasFilePathCache.getValue(filePath.str()); return m_hasFilePathCache.getValue(filePath);
} }
+2 -2
View File
@@ -4,7 +4,7 @@
#include <set> #include <set>
#include "utility/file/FileRegisterStateData.h" #include "utility/file/FileRegisterStateData.h"
#include "utility/Cache.h" #include "utility/OrderedCache.h"
class FileRegister class FileRegister
{ {
@@ -29,7 +29,7 @@ private:
const FilePath& m_currentPath; const FilePath& m_currentPath;
const std::set<FilePath> m_indexedPaths; const std::set<FilePath> m_indexedPaths;
const std::set<FilePath> m_excludedPaths; const std::set<FilePath> m_excludedPaths;
mutable Cache<std::string, bool> m_hasFilePathCache; mutable OrderedCache<FilePath, bool> m_hasFilePathCache;
}; };
#endif // FILE_REGISTER_H #endif // FILE_REGISTER_H
+3 -3
View File
@@ -4,10 +4,10 @@
#include <clang/AST/Decl.h> #include <clang/AST/Decl.h>
#include "data/name/NameHierarchy.h" #include "data/name/NameHierarchy.h"
#include "utility/Cache.h" #include "utility/UnorderedCache.h"
typedef Cache<const clang::NamedDecl*, NameHierarchy> DeclNameCache; typedef UnorderedCache<const clang::NamedDecl*, NameHierarchy> DeclNameCache;
typedef Cache<const clang::Type*, NameHierarchy> TypeNameCache; typedef UnorderedCache<const clang::Type*, NameHierarchy> TypeNameCache;
class CxxContext class CxxContext
{ {