Optimize ZMQ-JSON vector reading; GetBlocksFast reads 24%+ faster

This commit is contained in:
Lee Clagett 2020-05-30 00:58:03 -04:00
parent 60627c9f24
commit 98c151ecb8

View file

@ -32,6 +32,7 @@
#include <cstring> #include <cstring>
#include <rapidjson/document.h> #include <rapidjson/document.h>
#include <rapidjson/writer.h> #include <rapidjson/writer.h>
#include <vector>
#include "byte_stream.h" #include "byte_stream.h"
#include "cryptonote_basic/cryptonote_basic.h" #include "cryptonote_basic/cryptonote_basic.h"
@ -341,6 +342,7 @@ inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type from
auto itr = val.MemberBegin(); auto itr = val.MemberBegin();
map.clear();
while (itr != val.MemberEnd()) while (itr != val.MemberEnd())
{ {
typename Map::key_type k; typename Map::key_type k;
@ -361,6 +363,19 @@ inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type t
dest.EndArray(); dest.EndArray();
} }
namespace traits
{
template<typename T>
void reserve(const T&, std::size_t)
{}
template<typename T>
void reserve(std::vector<T>& vec, const std::size_t count)
{
vec.reserve(count);
}
}
template <typename Vec> template <typename Vec>
inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec) inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec)
{ {
@ -369,11 +384,12 @@ inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type f
throw WRONG_TYPE("json array"); throw WRONG_TYPE("json array");
} }
vec.clear();
traits::reserve(vec, val.Size());
for (rapidjson::SizeType i=0; i < val.Size(); i++) for (rapidjson::SizeType i=0; i < val.Size(); i++)
{ {
typename Vec::value_type v; vec.emplace_back();
fromJsonValue(val[i], v); fromJsonValue(val[i], vec.back());
vec.push_back(v);
} }
} }