From 98c151ecb8dd887ef9ade5442de467875b8f16f5 Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Sat, 30 May 2020 00:58:03 -0400 Subject: Optimize ZMQ-JSON vector reading; GetBlocksFast reads 24%+ faster --- src/serialization/json_object.h | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/serialization') diff --git a/src/serialization/json_object.h b/src/serialization/json_object.h index 1dc6451cf..b5d0f1508 100644 --- a/src/serialization/json_object.h +++ b/src/serialization/json_object.h @@ -32,6 +32,7 @@ #include #include #include +#include #include "byte_stream.h" #include "cryptonote_basic/cryptonote_basic.h" @@ -341,6 +342,7 @@ inline typename std::enable_if::value, void>::type from auto itr = val.MemberBegin(); + map.clear(); while (itr != val.MemberEnd()) { typename Map::key_type k; @@ -361,6 +363,19 @@ inline typename std::enable_if::value, void>::type t dest.EndArray(); } +namespace traits +{ + template + void reserve(const T&, std::size_t) + {} + + template + void reserve(std::vector& vec, const std::size_t count) + { + vec.reserve(count); + } +} + template inline typename std::enable_if::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec) { @@ -369,11 +384,12 @@ inline typename std::enable_if::value, void>::type f throw WRONG_TYPE("json array"); } + vec.clear(); + traits::reserve(vec, val.Size()); for (rapidjson::SizeType i=0; i < val.Size(); i++) { - typename Vec::value_type v; - fromJsonValue(val[i], v); - vec.push_back(v); + vec.emplace_back(); + fromJsonValue(val[i], vec.back()); } } -- cgit v1.2.3