From 5d23fd99af8a3df6ac4f700518a7bb4981da90a0 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 10 Oct 2020 20:41:04 -0700 Subject: [PATCH 1/3] Used shorter protobuf:: namespace alias. --- BUILD | 1 + tests/benchmark.cc | 62 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/BUILD b/BUILD index 90aa7a7919..a7d106bfc4 100644 --- a/BUILD +++ b/BUILD @@ -357,6 +357,7 @@ cc_binary( ":descriptor_upb_proto", ":descriptor_upbreflection", "@com_github_google_benchmark//:benchmark_main", + "@com_google_protobuf//:protobuf", ], ) diff --git a/tests/benchmark.cc b/tests/benchmark.cc index 682ae97ba2..f8e8441f16 100644 --- a/tests/benchmark.cc +++ b/tests/benchmark.cc @@ -3,8 +3,10 @@ #include #include "google/protobuf/descriptor.upb.h" #include "google/protobuf/descriptor.upbdefs.h" +#include "google/protobuf/descriptor.pb.h" upb_strview descriptor = google_protobuf_descriptor_proto_upbdefinit.descriptor; +namespace protobuf = ::google::protobuf; /* A buffer big enough to parse descriptor.proto without going to heap. */ char buf[65535]; @@ -27,7 +29,7 @@ static void BM_ArenaInitialBlockOneAlloc(benchmark::State& state) { } BENCHMARK(BM_ArenaInitialBlockOneAlloc); -static void BM_ParseDescriptorNoHeap(benchmark::State& state) { +static void BM_ParseDescriptor_Upb_LargeInitialBlock(benchmark::State& state) { size_t bytes = 0; for (auto _ : state) { upb_arena* arena = upb_arena_init(buf, sizeof(buf), NULL); @@ -43,9 +45,9 @@ static void BM_ParseDescriptorNoHeap(benchmark::State& state) { } state.SetBytesProcessed(state.iterations() * descriptor.size); } -BENCHMARK(BM_ParseDescriptorNoHeap); +BENCHMARK(BM_ParseDescriptor_Upb_LargeInitialBlock); -static void BM_ParseDescriptor(benchmark::State& state) { +static void BM_ParseDescriptor_Upb(benchmark::State& state) { size_t bytes = 0; for (auto _ : state) { upb_arena* arena = upb_arena_new(); @@ -61,9 +63,55 @@ static void BM_ParseDescriptor(benchmark::State& state) { } state.SetBytesProcessed(state.iterations() * descriptor.size); } -BENCHMARK(BM_ParseDescriptor); +BENCHMARK(BM_ParseDescriptor_Upb); -static void BM_SerializeDescriptor(benchmark::State& state) { +static void BM_ParseDescriptor_Proto2_NoArena(benchmark::State& state) { + size_t bytes = 0; + for (auto _ : state) { + protobuf::FileDescriptorProto proto; + bool ok = proto.ParseFromArray(descriptor.data, descriptor.size); + + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptor_Proto2_NoArena); + +static void BM_ParseDescriptor_Proto2_WithArena(benchmark::State& state) { + size_t bytes = 0; + for (auto _ : state) { + protobuf::Arena arena; + auto proto = protobuf::Arena::CreateMessage( + &arena); + bool ok = proto->ParseFromArray(descriptor.data, descriptor.size); + + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptor_Proto2_WithArena); + +static void BM_SerializeDescriptor_Proto2(benchmark::State& state) { + size_t bytes = 0; + protobuf::FileDescriptorProto proto; + proto.ParseFromArray(descriptor.data, descriptor.size); + for (auto _ : state) { + proto.SerializeToArray(buf, sizeof(buf)); + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_SerializeDescriptor_Proto2); + +static void BM_SerializeDescriptor_Upb(benchmark::State& state) { int64_t total = 0; upb_arena* arena = upb_arena_new(); google_protobuf_FileDescriptorProto* set = @@ -83,6 +131,6 @@ static void BM_SerializeDescriptor(benchmark::State& state) { } total += size; } - state.SetBytesProcessed(state.iterations() * descriptor.size); + state.SetBytesProcessed(total); } -BENCHMARK(BM_SerializeDescriptor); +BENCHMARK(BM_SerializeDescriptor_Upb); From 30f01afa83fde84bcd1629315f702d015900d816 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 10 Oct 2020 20:47:01 -0700 Subject: [PATCH 2/3] Added LargeInitialBlock test for proto2. --- tests/benchmark.cc | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/benchmark.cc b/tests/benchmark.cc index f8e8441f16..eb7fdbba0d 100644 --- a/tests/benchmark.cc +++ b/tests/benchmark.cc @@ -81,7 +81,7 @@ static void BM_ParseDescriptor_Proto2_NoArena(benchmark::State& state) { } BENCHMARK(BM_ParseDescriptor_Proto2_NoArena); -static void BM_ParseDescriptor_Proto2_WithArena(benchmark::State& state) { +static void BM_ParseDescriptor_Proto2_Arena(benchmark::State& state) { size_t bytes = 0; for (auto _ : state) { protobuf::Arena arena; @@ -97,7 +97,28 @@ static void BM_ParseDescriptor_Proto2_WithArena(benchmark::State& state) { } state.SetBytesProcessed(state.iterations() * descriptor.size); } -BENCHMARK(BM_ParseDescriptor_Proto2_WithArena); +BENCHMARK(BM_ParseDescriptor_Proto2_Arena); + +static void BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock(benchmark::State& state) { + size_t bytes = 0; + protobuf::ArenaOptions opts; + opts.initial_block = buf; + opts.initial_block_size = sizeof(buf); + for (auto _ : state) { + protobuf::Arena arena(opts); + auto proto = protobuf::Arena::CreateMessage( + &arena); + bool ok = proto->ParsePartialFromArray(descriptor.data, descriptor.size); + + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock); static void BM_SerializeDescriptor_Proto2(benchmark::State& state) { size_t bytes = 0; From bc301e7da4f266e9441ee0c485e6d3c24667de62 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 10 Oct 2020 21:26:58 -0700 Subject: [PATCH 3/3] Use merge/partial variants to give proto2 benchmark the fairest hearing. --- tests/benchmark.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/benchmark.cc b/tests/benchmark.cc index eb7fdbba0d..15544afe1b 100644 --- a/tests/benchmark.cc +++ b/tests/benchmark.cc @@ -69,8 +69,8 @@ static void BM_ParseDescriptor_Proto2_NoArena(benchmark::State& state) { size_t bytes = 0; for (auto _ : state) { protobuf::FileDescriptorProto proto; - bool ok = proto.ParseFromArray(descriptor.data, descriptor.size); - + protobuf::StringPiece input(descriptor.data,descriptor.size); + bool ok = proto.ParseFrom(input); if (!ok) { printf("Failed to parse.\n"); exit(1); @@ -85,9 +85,10 @@ static void BM_ParseDescriptor_Proto2_Arena(benchmark::State& state) { size_t bytes = 0; for (auto _ : state) { protobuf::Arena arena; + protobuf::StringPiece input(descriptor.data,descriptor.size); auto proto = protobuf::Arena::CreateMessage( &arena); - bool ok = proto->ParseFromArray(descriptor.data, descriptor.size); + bool ok = proto->ParseFrom(input); if (!ok) { printf("Failed to parse.\n"); @@ -106,9 +107,10 @@ static void BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock(benchmark::State& opts.initial_block_size = sizeof(buf); for (auto _ : state) { protobuf::Arena arena(opts); + protobuf::StringPiece input(descriptor.data,descriptor.size); auto proto = protobuf::Arena::CreateMessage( &arena); - bool ok = proto->ParsePartialFromArray(descriptor.data, descriptor.size); + bool ok = proto->ParseFrom(input); if (!ok) { printf("Failed to parse.\n"); @@ -125,7 +127,7 @@ static void BM_SerializeDescriptor_Proto2(benchmark::State& state) { protobuf::FileDescriptorProto proto; proto.ParseFromArray(descriptor.data, descriptor.size); for (auto _ : state) { - proto.SerializeToArray(buf, sizeof(buf)); + proto.SerializePartialToArray(buf, sizeof(buf)); bytes += descriptor.size; } state.SetBytesProcessed(state.iterations() * descriptor.size);