diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-03-30 12:24:11 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-03-30 12:24:45 +0000 |
commit | 0423c53c743ad20504995cc5013c49d37d890741 (patch) | |
tree | c6aa5d433be6dc890adeee95c38330c9d9309ff4 /staging_vespalib | |
parent | c098a56abdf64cb98554dc91592ca1b409e92540 (diff) |
Avoid double lookup.
Diffstat (limited to 'staging_vespalib')
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h | 13 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.hpp | 11 |
2 files changed, 12 insertions, 12 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h index 777230566f2..7477ba82f33 100644 --- a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h +++ b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h @@ -108,7 +108,7 @@ public: /** * This fetches the object without modifying the lru list. */ - const V & get(const K & key) { return HashTable::find(key)->second._value; } + const V & get(const K & key) const { return HashTable::find(key)->second._value; } /** * This simply erases the object. @@ -133,13 +133,11 @@ public: insert_result insert(const K & key, V && value); /** - * Return the object with the given key. If it does not exist an empty one will be created. - * This can be used as an insert. - * Object is then put at head of LRU list. + * Return pointre to the object with the given key. + * Object is then put at head of LRU list if found. + * If not found nullptr is returned. */ - const V & operator [] (const K & key) const { - return const_cast<lrucache_map<P> *>(this)->findAndRef(key).second._value; - } + V * findAndRef(const K & key); /** * Return the object with the given key. If it does not exist an empty one will be created. @@ -183,7 +181,6 @@ private: * Implements the resize of the hashtable */ void move(NodeStore && oldStore) override; - internal_iterator findAndRef(const K & key); void ref(const internal_iterator & it); insert_result insert(value_type && value); void removeOld(); diff --git a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.hpp b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.hpp index c8a3db878b8..839a93cc5ca 100644 --- a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.hpp +++ b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.hpp @@ -263,14 +263,17 @@ lrucache_map<P>::operator [] (const K & key) } template< typename P > -typename lrucache_map<P>::internal_iterator +typename P::Value * lrucache_map<P>::findAndRef(const K & key) { internal_iterator found = HashTable::find(key); - if (found != HashTable::end() && (size()*2 > capacity())) { - ref(found); + if (found != HashTable::end()) { + if (size()*2 > capacity()) { + ref(found); + } + return &found->second._value; } - return found; + return nullptr; } } |