From 67f24ed0b2874df6eb2decb158176820e5720f2f Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Wed, 10 Jan 2024 15:18:30 +0800 Subject: [PATCH] [struct_pack][fix] fix benchmark --- include/ylt/struct_pack/unpacker.hpp | 2 +- src/struct_pack/benchmark/benchmark.cpp | 4 ++-- src/struct_pack/benchmark/config.hpp | 6 +++--- src/struct_pack/benchmark/data_def.hpp | 2 +- src/struct_pack/benchmark/flatbuffer_sample.hpp | 2 +- src/struct_pack/benchmark/msgpack_sample.hpp | 13 +++++++------ src/struct_pack/benchmark/protobuf_sample.hpp | 9 +++++---- src/struct_pack/benchmark/struct_pack_sample.hpp | 13 +++++++------ src/struct_pack/benchmark/struct_pb_sample.hpp | 10 ++++++---- 9 files changed, 33 insertions(+), 28 deletions(-) diff --git a/include/ylt/struct_pack/unpacker.hpp b/include/ylt/struct_pack/unpacker.hpp index 7bd346d2d..a724b13ca 100644 --- a/include/ylt/struct_pack/unpacker.hpp +++ b/include/ylt/struct_pack/unpacker.hpp @@ -652,7 +652,7 @@ class unpacker { if (!(vec[index / 8] & (0b1 << (index % 8)))) return {}; if constexpr (!no_skip) { - reader_.ignore(real_width) ? errc{} : errc::no_buffer_space; + return reader_.ignore(real_width) ? errc{} : errc::no_buffer_space; } else { bool ec{}; diff --git a/src/struct_pack/benchmark/benchmark.cpp b/src/struct_pack/benchmark/benchmark.cpp index b38565014..f32e2e160 100644 --- a/src/struct_pack/benchmark/benchmark.cpp +++ b/src/struct_pack/benchmark/benchmark.cpp @@ -90,7 +90,7 @@ void run_benchmark(const T& map, LibType base_line_type) { calculate_ser_rate(map, base_line_type, SampleType::RECTS, SampleType::RECTS); calculate_ser_rate(map, base_line_type, SampleType::VAR_RECTS, SampleType::RECTS); -#if __cplusplus >= 202002L +#if __cpp_lib_span >= 202002L calculate_ser_rate(map, base_line_type, SampleType::ZC_RECTS, SampleType::RECTS); #endif @@ -104,7 +104,7 @@ void run_benchmark(const T& map, LibType base_line_type) { SampleType::MONSTER); calculate_ser_rate(map, base_line_type, SampleType::MONSTERS, SampleType::MONSTERS); -#if __cplusplus >= 202002L +#if __cpp_lib_span >= 202002L calculate_ser_rate(map, base_line_type, SampleType::ZC_MONSTERS, SampleType::MONSTERS); #endif diff --git a/src/struct_pack/benchmark/config.hpp b/src/struct_pack/benchmark/config.hpp index 1d40a8680..ab80af1d8 100644 --- a/src/struct_pack/benchmark/config.hpp +++ b/src/struct_pack/benchmark/config.hpp @@ -6,7 +6,7 @@ #include #include inline constexpr int OBJECT_COUNT = 20; -inline constexpr int ITERATIONS = 100000; +inline constexpr int ITERATIONS = 1000000; enum class LibType { STRUCT_PACK, @@ -32,9 +32,9 @@ enum class SampleType { inline const std::unordered_map g_sample_name_map = { {SampleType::RECT, "1 rect"}, {SampleType::RECTS, std::to_string(OBJECT_COUNT) + " rects"}, - {SampleType::VAR_RECT, "1 rect(with fast varint edcode)"}, + {SampleType::VAR_RECT, "1 rect(with fast varint encode)"}, {SampleType::VAR_RECTS, - std::to_string(OBJECT_COUNT) + " rects(with fast varint edcode)"}, + std::to_string(OBJECT_COUNT) + " rects(with fast varint encode)"}, {SampleType::ZC_RECTS, std::to_string(OBJECT_COUNT) + " rects(with zero-copy deserialize)"}, {SampleType::PERSON, "1 person"}, diff --git a/src/struct_pack/benchmark/data_def.hpp b/src/struct_pack/benchmark/data_def.hpp index 7c820d46a..27eef9fc3 100644 --- a/src/struct_pack/benchmark/data_def.hpp +++ b/src/struct_pack/benchmark/data_def.hpp @@ -144,7 +144,7 @@ struct Monster { equipped, path); #endif }; -#if __cplusplus >= 202002L +#if __cpp_lib_span >= 202002L struct zc_Weapon { std::string_view name; int16_t damage; diff --git a/src/struct_pack/benchmark/flatbuffer_sample.hpp b/src/struct_pack/benchmark/flatbuffer_sample.hpp index d4a476145..fdb30cb39 100644 --- a/src/struct_pack/benchmark/flatbuffer_sample.hpp +++ b/src/struct_pack/benchmark/flatbuffer_sample.hpp @@ -201,7 +201,6 @@ struct flatbuffer_sample_t : public base_sample { uint64_t ns = 0; std::string bench_name = name() + " serialize " + get_sample_name(sample_type); - { ScopedTimer timer(bench_name.data(), ns); for (int i = 0; i < ITERATIONS; ++i) { @@ -228,6 +227,7 @@ struct flatbuffer_sample_t : public base_sample { auto obj = flatbuffers::GetRoot(builder.GetBufferPointer()); no_op((char *)obj); + no_op((char *)&builder); } } deser_time_elapsed_map_.emplace(sample_type, ns); diff --git a/src/struct_pack/benchmark/msgpack_sample.hpp b/src/struct_pack/benchmark/msgpack_sample.hpp index ad7523379..6ee764993 100644 --- a/src/struct_pack/benchmark/msgpack_sample.hpp +++ b/src/struct_pack/benchmark/msgpack_sample.hpp @@ -62,6 +62,7 @@ struct message_pack_sample : public base_sample { buffer_.clear(); msgpack::pack(buffer_, sample); no_op(buffer_.data()); + no_op((char *)&sample); } } ser_time_elapsed_map_.emplace(sample_type, ns); @@ -75,9 +76,8 @@ struct message_pack_sample : public base_sample { buffer_.clear(); msgpack::pack(buffer_, sample); - std::vector vec; - vec.resize(ITERATIONS); - + msgpack::unpacked vec; + T val; uint64_t ns = 0; std::string bench_name = name() + " deserialize " + get_sample_name(sample_type); @@ -85,10 +85,11 @@ struct message_pack_sample : public base_sample { { ScopedTimer timer(bench_name.data(), ns); for (int i = 0; i < ITERATIONS; ++i) { - msgpack::unpack(vec[i], buffer_.data(), buffer_.size()); - vec[i].get().as(); + msgpack::unpack(vec, buffer_.data(), buffer_.size()); + val = vec->as(); + no_op((char *)&val); + no_op(buffer_.data()); } - no_op((char *)vec.data()); } deser_time_elapsed_map_.emplace(sample_type, ns); } diff --git a/src/struct_pack/benchmark/protobuf_sample.hpp b/src/struct_pack/benchmark/protobuf_sample.hpp index 7c74c8b1a..72f98b356 100644 --- a/src/struct_pack/benchmark/protobuf_sample.hpp +++ b/src/struct_pack/benchmark/protobuf_sample.hpp @@ -197,6 +197,7 @@ struct protobuf_sample_t : public base_sample { buffer_.clear(); sample.SerializeToString(&buffer_); no_op(buffer_); + no_op((char *)&sample); } } ser_time_elapsed_map_.emplace(sample_type, ns); @@ -210,8 +211,7 @@ struct protobuf_sample_t : public base_sample { buffer_.clear(); sample.SerializeToString(&buffer_); - std::vector vec; - vec.resize(ITERATIONS); + T obj; uint64_t ns = 0; std::string bench_name = @@ -220,9 +220,10 @@ struct protobuf_sample_t : public base_sample { { ScopedTimer timer(bench_name.data(), ns); for (int i = 0; i < ITERATIONS; ++i) { - vec[i].ParseFromString(buffer_); + obj.ParseFromString(buffer_); + no_op((char *)&obj); + no_op(buffer_); } - no_op((char *)vec.data()); } deser_time_elapsed_map_.emplace(sample_type, ns); } diff --git a/src/struct_pack/benchmark/struct_pack_sample.hpp b/src/struct_pack/benchmark/struct_pack_sample.hpp index a34bdb2c4..1b19bd746 100644 --- a/src/struct_pack/benchmark/struct_pack_sample.hpp +++ b/src/struct_pack/benchmark/struct_pack_sample.hpp @@ -102,7 +102,7 @@ struct struct_pack_sample : public base_sample { deserialize(SampleType::RECTS, rects_); deserialize(SampleType::VAR_RECT, rect2s_[0]); deserialize(SampleType::VAR_RECTS, rect2s_); -#if __cplusplus >= 202002L +#if __cpp_lib_span >= 202002L auto sp = std::span{rects_}; deserialize(SampleType::ZC_RECTS, sp); #endif @@ -112,7 +112,7 @@ struct struct_pack_sample : public base_sample { SampleType::ZC_PERSONS, persons_); deserialize(SampleType::MONSTER, monsters_[0]); deserialize(SampleType::MONSTERS, monsters_); -#if __cplusplus >= 202002L +#if __cpp_lib_span >= 202002L deserialize, std::vector>( SampleType::ZC_MONSTERS, monsters_); #endif @@ -135,6 +135,7 @@ struct struct_pack_sample : public base_sample { buffer_.clear(); struct_pack::serialize_to(buffer_, sample); no_op(buffer_); + no_op((char *)&sample); } } ser_time_elapsed_map_.emplace(sample_type, ns); @@ -147,8 +148,7 @@ struct struct_pack_sample : public base_sample { buffer_.clear(); struct_pack::serialize_to(buffer_, sample); - std::vector vec; - vec.resize(ITERATIONS); + U obj; uint64_t ns = 0; std::string bench_name = @@ -157,9 +157,10 @@ struct struct_pack_sample : public base_sample { { ScopedTimer timer(bench_name.data(), ns); for (int i = 0; i < ITERATIONS; ++i) { - [[maybe_unused]] auto ec = struct_pack::deserialize_to(vec[i], buffer_); + [[maybe_unused]] auto ec = struct_pack::deserialize_to(obj, buffer_); + no_op((char *)&obj); + no_op(buffer_); } - no_op((char *)vec.data()); } deser_time_elapsed_map_.emplace(sample_type, ns); } diff --git a/src/struct_pack/benchmark/struct_pb_sample.hpp b/src/struct_pack/benchmark/struct_pb_sample.hpp index b58bbbab7..bfec902d0 100644 --- a/src/struct_pack/benchmark/struct_pb_sample.hpp +++ b/src/struct_pack/benchmark/struct_pb_sample.hpp @@ -140,6 +140,7 @@ struct struct_pb_sample_t : public base_sample { struct_pb::internal::serialize_to(buffer_.data(), buffer_.size(), sample); no_op(buffer_); + no_op((char*)&sample); } } @@ -154,8 +155,8 @@ struct struct_pb_sample_t : public base_sample { buffer_.resize(sz); struct_pb::internal::serialize_to(buffer_.data(), buffer_.size(), sample); - std::vector vec; - vec.resize(ITERATIONS); + T obj; + // vec.resize(ITERATIONS); uint64_t ns = 0; std::string bench_name = @@ -165,10 +166,11 @@ struct struct_pb_sample_t : public base_sample { ScopedTimer timer(bench_name.data(), ns); for (int i = 0; i < ITERATIONS; ++i) { [[maybe_unused]] auto ok = struct_pb::internal::deserialize_to( - vec[i], buffer_.data(), buffer_.size()); + obj, buffer_.data(), buffer_.size()); assert(ok); + no_op((char*)&obj); + no_op(buffer_); } - no_op((char*)vec.data()); } deser_time_elapsed_map_.emplace(sample_type, ns);