From: Ning Yao Date: Sun, 13 Dec 2015 05:40:23 +0000 (+0800) Subject: common: improve cache, replace to unordered map with initializing hash bucket size X-Git-Tag: v10.0.3~207^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c3a383c341719dec7072bd14cbda27e5dc006e46;p=ceph.git common: improve cache, replace to unordered map with initializing hash bucket size Based on the PR https://github.com/ceph/ceph/pull/4441 Signed-off-by: Ning Yao --- diff --git a/src/common/shared_cache.hpp b/src/common/shared_cache.hpp index c55119c02b79..bba61a3336c1 100644 --- a/src/common/shared_cache.hpp +++ b/src/common/shared_cache.hpp @@ -21,8 +21,9 @@ #include #include "common/Mutex.h" #include "common/Cond.h" +#include "include/unordered_map.h" -template > +template , class H = std::hash > class SharedLRU { CephContext *cct; typedef ceph::shared_ptr VPtr; @@ -34,7 +35,7 @@ class SharedLRU { public: int waiting; private: - map >::iterator, C> contents; + ceph::unordered_map >::iterator, H> contents; list > lru; map, C> weak_refs; @@ -47,7 +48,7 @@ private: } void lru_remove(const K& key) { - typename map >::iterator, C>::iterator i = + typename ceph::unordered_map >::iterator, H>::iterator i = contents.find(key); if (i == contents.end()) return; @@ -57,7 +58,7 @@ private: } void lru_add(const K& key, const VPtr& val, list *to_release) { - typename map >::iterator, C>::iterator i = + typename ceph::unordered_map >::iterator, H>::iterator i = contents.find(key); if (i != contents.end()) { lru.splice(lru.begin(), lru, i->second); @@ -92,7 +93,9 @@ private: public: SharedLRU(CephContext *cct = NULL, size_t max_size = 20) : cct(cct), lock("SharedLRU::lock"), max_size(max_size), - size(0), waiting(0) {} + size(0), waiting(0) { + contents.rehash(max_size); + } ~SharedLRU() { contents.clear(); diff --git a/src/common/simple_cache.hpp b/src/common/simple_cache.hpp index 8038306adf8b..548f84614e41 100644 --- a/src/common/simple_cache.hpp +++ b/src/common/simple_cache.hpp @@ -20,12 +20,13 @@ #include #include "common/Mutex.h" #include "common/Cond.h" +#include "include/unordered_map.h" -template > +template , class H = std::hash > class SimpleLRU { Mutex lock; size_t max_size; - map >::iterator, C> contents; + ceph::unordered_map >::iterator, H> contents; list > lru; map 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); @@ -64,7 +67,7 @@ public: void clear(K key) { Mutex::Locker l(lock); - typename map >::iterator, C>::iterator i = + typename ceph::unordered_map >::iterator, H>::iterator i = contents.find(key); if (i == contents.end()) return;