]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: improve cache efficiency 6909/head
authorNing Yao <zay11022@gmail.com>
Sun, 13 Dec 2015 06:26:00 +0000 (14:26 +0800)
committerNing Yao <zay11022@gmail.com>
Sun, 13 Dec 2015 15:54:59 +0000 (23:54 +0800)
if (weak_refs.count(key))
  weak_refs[key]
it traverses the b-tree twice
replace it with weak_refs.find(key), which traverses b-tree once

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

index bba61a3336c18d6c76ecb98ca9712606449d8cdd..8ff949084a6ebd7713841a02b69da3088363a24b 100644 (file)
@@ -164,8 +164,9 @@ public:
     VPtr val; // release any ref we have after we drop the lock
     {
       Mutex::Locker l(lock);
-      if (weak_refs.count(key)) {
-       val = weak_refs[key].first.lock();
+      typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
+      if (i != weak_refs.end()) {
+       val = i->second.first.lock();
       }
       lru_remove(key);
     }
@@ -175,11 +176,12 @@ public:
     VPtr val; // release any ref we have after we drop the lock
     {
       Mutex::Locker l(lock);
-      if (weak_refs.count(key)) {
-       val = weak_refs[key].first.lock();
+      typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
+      if (i != weak_refs.end()) {
+       val = i->second.first.lock();
+        weak_refs.erase(i);
       }
       lru_remove(key);
-      weak_refs.erase(key);
     }
   }
 
index 548f84614e41d88a25621eaf24bf493df5d375cf..362763253becff3bf64ea1fbc4f914a6c6867ad8 100644 (file)
@@ -58,10 +58,12 @@ 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);
     }
   }
 
@@ -83,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;