From aeb7a2af5dc546d28c373b8f78aadef78823c2d2 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 8 Jan 2013 16:16:20 -0800 Subject: [PATCH] Optimize for the more common case. --- src/cpp/ripple/TaggedCache.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cpp/ripple/TaggedCache.h b/src/cpp/ripple/TaggedCache.h index 344425c4b..e294e0591 100644 --- a/src/cpp/ripple/TaggedCache.h +++ b/src/cpp/ripple/TaggedCache.h @@ -286,6 +286,14 @@ boost::shared_ptr TaggedCache::fetch(const key_type& key) { // fetch us a shared pointer to the stored data object boost::recursive_mutex::scoped_lock sl(mLock); + // Is it in the cache? + cache_iterator cit = mCache.find(key); + if (cit != mCache.end()) + { + cit->second.first = time(NULL); // Yes, refresh + return cit->second.second; + } + // Is it in the map? map_iterator mit = mMap.find(key); if (mit == mMap.end()) @@ -298,13 +306,8 @@ boost::shared_ptr TaggedCache::fetch(const key_type& key) return cachedData; } - // Valid in map, is it in the cache? - cache_iterator cit = mCache.find(key); - if (cit != mCache.end()) - cit->second.first = time(NULL); // Yes, refresh - else // No, add to cache - mCache.insert(cache_pair(key, cache_entry(time(NULL), cachedData))); - + // Put it back in the cache + mCache.insert(cache_pair(key, cache_entry(time(NULL), cachedData))); return cachedData; }