From e8b385c22058c1d17a77243055abe0e58da55b1e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 13 Dec 2019 23:03:50 -0500
Subject: [PATCH] utils: optimize findMapValue

---
 include/opendht/utils.h |  2 +-
 src/utils.cpp           |  4 ++--
 src/value.cpp           | 14 --------------
 3 files changed, 3 insertions(+), 17 deletions(-)

diff --git a/include/opendht/utils.h b/include/opendht/utils.h
index 225d8e2f..d7c1783c 100644
--- a/include/opendht/utils.h
+++ b/include/opendht/utils.h
@@ -153,6 +153,6 @@ unpackMsg(Blob b) {
 
 msgpack::unpacked unpackMsg(Blob b);
 
-msgpack::object* findMapValue(msgpack::object& map, const std::string& key);
+msgpack::object* findMapValue(const msgpack::object& map, const char* key);
 
 } // namespace dht
diff --git a/src/utils.cpp b/src/utils.cpp
index 458b044d..e27f2ced 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -270,11 +270,11 @@ unpackMsg(Blob b) {
 }
 
 msgpack::object*
-findMapValue(msgpack::object& map, const std::string& key) {
+findMapValue(const msgpack::object& map, const char* key) {
     if (map.type != msgpack::type::MAP) throw msgpack::type_error();
     for (unsigned i = 0; i < map.via.map.size; i++) {
         auto& o = map.via.map.ptr[i];
-        if (o.key.type == msgpack::type::STR && o.key.as<std::string>() == key)
+        if (o.key.type == msgpack::type::STR && std::strncmp(o.key.via.str.ptr, key, o.key.via.str.size) == 0)
             return &o.val;
     }
     return nullptr;
diff --git a/src/value.cpp b/src/value.cpp
index 0750a530..3f8298eb 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -87,20 +87,6 @@ ValueType::DEFAULT_STORE_POLICY(InfoHash, const std::shared_ptr<Value>& v, const
     return v->size() <= MAX_VALUE_SIZE;
 }
 
-msgpack::object*
-findMapValue(const msgpack::object& map, const std::string& key) {
-    if (map.type != msgpack::type::MAP) throw msgpack::type_error();
-    for (unsigned i = 0; i < map.via.map.size; i++) {
-        auto& o = map.via.map.ptr[i];
-        if(o.key.type != msgpack::type::STR)
-            continue;
-        if (o.key.as<std::string>() == key) {
-            return &o.val;
-        }
-    }
-    return nullptr;
-}
-
 size_t
 Value::size() const
 {
-- 
GitLab