Skip to content

Commit

Permalink
Add benchmark for field lookup
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 702790181
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 4, 2024
1 parent 926f87d commit e519987
Show file tree
Hide file tree
Showing 3 changed files with 628 additions and 0 deletions.
31 changes: 31 additions & 0 deletions upb/mini_table/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

load(
"//bazel:upb_minitable_proto_library.bzl",
"upb_minitable_proto_library",
)
load(
"//upb/bazel:build_defs.bzl",
"UPB_DEFAULT_COPTS",
Expand Down Expand Up @@ -95,6 +99,33 @@ cc_test(
],
)

proto_library(
name = "message_benchmark_proto",
testonly = 1,
srcs = ["message_benchmark.proto"],
)

upb_minitable_proto_library(
name = "message_benchmark_upb_minitable_proto",
testonly = 1,
deps = [":message_benchmark_proto"],
)

cc_test(
name = "message_benchmark",
srcs = ["message_benchmark.cc"],
deps = [
":message_benchmark_upb_minitable_proto",
":mini_table",
"//upb:mini_table_compat",
"//upb:port",
"@com_github_google_benchmark//:benchmark_main",
"@com_google_absl//absl/random",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

filegroup(
name = "source_files",
srcs = glob(
Expand Down
35 changes: 35 additions & 0 deletions upb/mini_table/message_benchmark.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <cstdint>

#include <benchmark/benchmark.h>
#include "absl/random/random.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/message_benchmark.upb_minitable.h"
#include "upb/port/def.inc"

namespace {
static void BM_FindFieldByNumber(benchmark::State& state) {
uint32_t min, max;
if (state.range(0)) {
min = 1;
max = 169;
} else {
min = 171;
max = 552;
}
const upb_MiniTable* ptr =
third_0party_0upb_0upb_0mini_0table__TestManyFields_msg_init_ptr;
absl::BitGen bitgen;
uint32_t search[1024];
for (auto& s : search) {
s = absl::Uniform(bitgen, min, max);
}
uint32_t i = 0;
for (auto _ : state) {
uint16_t offset = upb_MiniTable_FindFieldByNumber(ptr, search[(i++ % 1024)])
->UPB_PRIVATE(offset);
benchmark::DoNotOptimize(offset);
}
}
BENCHMARK(BM_FindFieldByNumber)->Arg(true)->Arg(false);

} // namespace
Loading

0 comments on commit e519987

Please sign in to comment.