diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-03-30 15:03:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-30 15:03:07 +0200 |
commit | 6a8a554a9c6a86dbc3c2e66d4b8c9ac7ca7ec817 (patch) | |
tree | b2329be3d52901952e637079094e5b0dce4dac85 | |
parent | c098a56abdf64cb98554dc91592ca1b409e92540 (diff) | |
parent | 723dadd315d8d404aaf59ab37a5e00bbf1b0c62c (diff) |
Merge pull request #12764 from vespa-engine/balder/avoid-double-lookup
Avoid double lookup.
3 files changed, 15 insertions, 14 deletions
diff --git a/messagebus/src/vespa/messagebus/network/rpcservicepool.cpp b/messagebus/src/vespa/messagebus/network/rpcservicepool.cpp index cad28b9f601..fb40ccff62b 100644 --- a/messagebus/src/vespa/messagebus/network/rpcservicepool.cpp +++ b/messagebus/src/vespa/messagebus/network/rpcservicepool.cpp @@ -19,8 +19,9 @@ RPCServicePool::~RPCServicePool() = default; RPCServiceAddress::UP RPCServicePool::resolve(const string &pattern) { - if (_lru.hasKey(pattern)) { - return _lru[pattern]->resolve(); + std::unique_ptr<RPCService> * found = _lru.findAndRef(pattern); + if (found) { + return (*found)->resolve(); } else { auto service = std::make_unique<RPCService>(_net.getMirror(), pattern); auto result = service->resolve(); diff --git a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h index 777230566f2..ea5ccf0659b 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 pointer 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; } } |