]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: improve cache, replace to unordered map with initializing hash bucket size
authorNing Yao <zay11022@gmail.com>
Sun, 13 Dec 2015 05:40:23 +0000 (13:40 +0800)
committerNing Yao <zay11022@gmail.com>
Sun, 13 Dec 2015 15:54:48 +0000 (23:54 +0800)
Based on the PR https://github.com/ceph/ceph/pull/4441

Signed-off-by: Ning Yao <zay11022@gmail.com>
src/common/shared_cache.hpp
src/common/simple_cache.hpp

index c55119c02b796c79ff86104cde54311db53e3dd9..bba61a3336c18d6c76ecb98ca9712606449d8cdd 100644 (file)
@@ -21,8 +21,9 @@
 #include <utility>
 #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 SharedLRU {
   CephContext *cct;
   typedef ceph::shared_ptr<V> VPtr;
@@ -34,7 +35,7 @@ class SharedLRU {
 public:
   int waiting;
 private:
-  map<K, typename list<pair<K, VPtr> >::iterator, C> contents;
+  ceph::unordered_map<K, typename list<pair<K, VPtr> >::iterator, H> contents;
   list<pair<K, VPtr> > lru;
 
   map<K, pair<WeakVPtr, V*>, C> weak_refs;
@@ -47,7 +48,7 @@ private:
   }
 
   void lru_remove(const K& key) {
-    typename map<K, typename list<pair<K, VPtr> >::iterator, C>::iterator i =
+    typename ceph::unordered_map<K, typename list<pair<K, VPtr> >::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<VPtr> *to_release) {
-    typename map<K, typename list<pair<K, VPtr> >::iterator, C>::iterator i =
+    typename ceph::unordered_map<K, typename list<pair<K, VPtr> >::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();
index 8038306adf8b559fffa3ee31d84ae13feebb9302..548f84614e41d88a25621eaf24bf493df5d375cf 100644 (file)
 #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);
@@ -64,7 +67,7 @@ public:
 
   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;