Skip to content

Commit

Permalink
[struct_pack][fix] fix benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Jan 10, 2024
1 parent 73a0016 commit 67f24ed
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion include/ylt/struct_pack/unpacker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
4 changes: 2 additions & 2 deletions src/struct_pack/benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/struct_pack/benchmark/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <unordered_map>
#include <vector>
inline constexpr int OBJECT_COUNT = 20;
inline constexpr int ITERATIONS = 100000;
inline constexpr int ITERATIONS = 1000000;

enum class LibType {
STRUCT_PACK,
Expand All @@ -32,9 +32,9 @@ enum class SampleType {
inline const std::unordered_map<SampleType, std::string> 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"},
Expand Down
2 changes: 1 addition & 1 deletion src/struct_pack/benchmark/data_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/struct_pack/benchmark/flatbuffer_sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -228,6 +227,7 @@ struct flatbuffer_sample_t : public base_sample {
auto obj =
flatbuffers::GetRoot<fb::Monsters>(builder.GetBufferPointer());
no_op((char *)obj);
no_op((char *)&builder);
}
}
deser_time_elapsed_map_.emplace(sample_type, ns);
Expand Down
13 changes: 7 additions & 6 deletions src/struct_pack/benchmark/msgpack_sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -75,20 +76,20 @@ struct message_pack_sample : public base_sample {
buffer_.clear();
msgpack::pack(buffer_, sample);

std::vector<msgpack::unpacked> vec;
vec.resize(ITERATIONS);

msgpack::unpacked vec;
T val;
uint64_t ns = 0;
std::string bench_name =
name() + " deserialize " + get_sample_name(sample_type);

{
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<T>();
msgpack::unpack(vec, buffer_.data(), buffer_.size());
val = vec->as<T>();
no_op((char *)&val);
no_op(buffer_.data());
}
no_op((char *)vec.data());
}
deser_time_elapsed_map_.emplace(sample_type, ns);
}
Expand Down
9 changes: 5 additions & 4 deletions src/struct_pack/benchmark/protobuf_sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -210,8 +211,7 @@ struct protobuf_sample_t : public base_sample {
buffer_.clear();
sample.SerializeToString(&buffer_);

std::vector<T> vec;
vec.resize(ITERATIONS);
T obj;

uint64_t ns = 0;
std::string bench_name =
Expand All @@ -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);
}
Expand Down
13 changes: 7 additions & 6 deletions src/struct_pack/benchmark/struct_pack_sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Monster>, std::vector<zc_Monster>>(
SampleType::ZC_MONSTERS, monsters_);
#endif
Expand All @@ -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);
Expand All @@ -147,8 +148,7 @@ struct struct_pack_sample : public base_sample {
buffer_.clear();
struct_pack::serialize_to(buffer_, sample);

std::vector<U> vec;
vec.resize(ITERATIONS);
U obj;

uint64_t ns = 0;
std::string bench_name =
Expand All @@ -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);
}
Expand Down
10 changes: 6 additions & 4 deletions src/struct_pack/benchmark/struct_pb_sample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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<T> vec;
vec.resize(ITERATIONS);
T obj;
// vec.resize(ITERATIONS);

uint64_t ns = 0;
std::string bench_name =
Expand All @@ -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);

Expand Down

0 comments on commit 67f24ed

Please sign in to comment.