diff options
author | Sage Weil <sage@redhat.com> | 2015-12-18 21:06:14 +0100 |
---|---|---|
committer | Sage Weil <sage@redhat.com> | 2015-12-18 21:06:14 +0100 |
commit | 471164d61a50306d223418ae115b49bdd4da73dd (patch) | |
tree | 577e0f989b0c0a375aebe5783e737ecdbe09418b /src/common/simple_cache.hpp | |
parent | Merge pull request #6968 from xiexingguo/xxg-wip-14118 (diff) | |
parent | common: improve cache efficiency (diff) | |
download | ceph-471164d61a50306d223418ae115b49bdd4da73dd.tar.xz ceph-471164d61a50306d223418ae115b49bdd4da73dd.zip |
Merge pull request #6909 from mslovy/wip-shared-cache
common: improve shared_cache and simple_cache efficiency with hash table
Reviewed-by: Sage Weil <sage@redhat.com>
Diffstat (limited to 'src/common/simple_cache.hpp')
-rw-r--r-- | src/common/simple_cache.hpp | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/common/simple_cache.hpp b/src/common/simple_cache.hpp index 8038306adf8..362763253be 100644 --- a/src/common/simple_cache.hpp +++ b/src/common/simple_cache.hpp @@ -20,12 +20,13 @@ #include <memory> #include "common/Mutex.h" #include "common/Cond.h" +#include "include/unordered_map.h" -template <class K, class V, class C = std::less<K> > +template <class K, class V, class C = std::less<K>, class H = std::hash<K> > class SimpleLRU { Mutex lock; size_t max_size; - map<K, typename list<pair<K, V> >::iterator, C> contents; + ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H> contents; list<pair<K, V> > lru; map<K, V, C> pinned; @@ -43,7 +44,9 @@ class SimpleLRU { } public: - SimpleLRU(size_t max_size) : lock("SimpleLRU::lock"), max_size(max_size) {} + SimpleLRU(size_t max_size) : lock("SimpleLRU::lock"), max_size(max_size) { + contents.rehash(max_size); + } void pin(K key, V val) { Mutex::Locker l(lock); @@ -55,16 +58,18 @@ public: for (typename map<K, V, C>::iterator i = pinned.begin(); i != pinned.end() && i->first <= e; pinned.erase(i++)) { - if (!contents.count(i->first)) + typename ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H>::iterator iter = + contents.find(i->first); + if (iter == contents.end()) _add(i->first, i->second); else - lru.splice(lru.begin(), lru, contents[i->first]); + lru.splice(lru.begin(), lru, iter->second); } } void clear(K key) { Mutex::Locker l(lock); - typename map<K, typename list<pair<K, V> >::iterator, C>::iterator i = + typename ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H>::iterator i = contents.find(key); if (i == contents.end()) return; @@ -80,15 +85,16 @@ public: bool lookup(K key, V *out) { Mutex::Locker l(lock); - typename list<pair<K, V> >::iterator loc = contents.count(key) ? - contents[key] : lru.end(); - if (loc != lru.end()) { - *out = loc->second; - lru.splice(lru.begin(), lru, loc); + typename ceph::unordered_map<K, typename list<pair<K, V> >::iterator, H>::iterator i = + contents.find(key); + if (i != contents.end()) { + *out = i->second->second; + lru.splice(lru.begin(), lru, i->second); return true; } - if (pinned.count(key)) { - *out = pinned[key]; + typename map<K, V, C>::iterator i_pinned = pinned.find(key); + if (i_pinned != pinned.end()) { + *out = i_pinned->second; return true; } return false; |