From: Ning Yao Date: Sun, 13 Dec 2015 06:26:00 +0000 (+0800) Subject: common: improve cache efficiency X-Git-Tag: v10.0.3~207^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=74c249d70d1152f372c0cf1937954e1117a8ddd4;p=ceph.git common: improve cache efficiency 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 --- diff --git a/src/common/shared_cache.hpp b/src/common/shared_cache.hpp index bba61a3336c1..8ff949084a6e 100644 --- a/src/common/shared_cache.hpp +++ b/src/common/shared_cache.hpp @@ -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, 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, 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); } } diff --git a/src/common/simple_cache.hpp b/src/common/simple_cache.hpp index 548f84614e41..362763253bec 100644 --- a/src/common/simple_cache.hpp +++ b/src/common/simple_cache.hpp @@ -58,10 +58,12 @@ public: for (typename map::iterator i = pinned.begin(); i != pinned.end() && i->first <= e; pinned.erase(i++)) { - if (!contents.count(i->first)) + typename ceph::unordered_map >::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 >::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 >::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::iterator i_pinned = pinned.find(key); + if (i_pinned != pinned.end()) { + *out = i_pinned->second; return true; } return false;