Skip to content
Snippets Groups Projects
Commit e8b385c2 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

utils: optimize findMapValue

parent c1e2a368
No related branches found
No related tags found
No related merge requests found
...@@ -153,6 +153,6 @@ unpackMsg(Blob b) { ...@@ -153,6 +153,6 @@ unpackMsg(Blob b) {
msgpack::unpacked 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 } // namespace dht
...@@ -270,11 +270,11 @@ unpackMsg(Blob b) { ...@@ -270,11 +270,11 @@ unpackMsg(Blob b) {
} }
msgpack::object* 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(); if (map.type != msgpack::type::MAP) throw msgpack::type_error();
for (unsigned i = 0; i < map.via.map.size; i++) { for (unsigned i = 0; i < map.via.map.size; i++) {
auto& o = map.via.map.ptr[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 &o.val;
} }
return nullptr; return nullptr;
......
...@@ -87,20 +87,6 @@ ValueType::DEFAULT_STORE_POLICY(InfoHash, const std::shared_ptr<Value>& v, const ...@@ -87,20 +87,6 @@ ValueType::DEFAULT_STORE_POLICY(InfoHash, const std::shared_ptr<Value>& v, const
return v->size() <= MAX_VALUE_SIZE; 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 size_t
Value::size() const Value::size() const
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment