benchmarks: remove more boilerplate.
Many of our benchmarks are basically just "call one function with a
fixed argument". We don't need to keep repeating all the boilerplate for
that.
This also ensures we don't forget the benchmark::DoNotOptimize, which --
in addition to being a good idea in general -- specifically solves the
problem with gettid benchmark and provides a more accurate result by
removing the indirection through a function pointer.
Test: ran benchmarks
Change-Id: Id67535243678cd0d48f51cf25141e2040da9af03
diff --git a/benchmarks/inttypes_benchmark.cpp b/benchmarks/inttypes_benchmark.cpp
index f123eb8..b96384e 100644
--- a/benchmarks/inttypes_benchmark.cpp
+++ b/benchmarks/inttypes_benchmark.cpp
@@ -19,16 +19,5 @@
#include <benchmark/benchmark.h>
#include "util.h"
-void BM_inttypes_strtoimax(benchmark::State& state) {
- while (state.KeepRunning()) {
- strtoimax(" -123", nullptr, 0);
- }
-}
-BIONIC_BENCHMARK(BM_inttypes_strtoimax);
-
-void BM_inttypes_strtoumax(benchmark::State& state) {
- while (state.KeepRunning()) {
- strtoumax(" -123", nullptr, 0);
- }
-}
-BIONIC_BENCHMARK(BM_inttypes_strtoumax);
+BIONIC_TRIVIAL_BENCHMARK(BM_inttypes_strtoimax, strtoimax(" -123", nullptr, 0));
+BIONIC_TRIVIAL_BENCHMARK(BM_inttypes_strtoumax, strtoumax(" -123", nullptr, 0));
diff --git a/benchmarks/stdlib_benchmark.cpp b/benchmarks/stdlib_benchmark.cpp
index 6afe7aa..ec3f6f2 100644
--- a/benchmarks/stdlib_benchmark.cpp
+++ b/benchmarks/stdlib_benchmark.cpp
@@ -216,45 +216,9 @@
}
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc, "0");
-void BM_stdlib_atoi(benchmark::State& state) {
- for (auto _ : state) {
- benchmark::DoNotOptimize(atoi(" -123"));
- }
-}
-BIONIC_BENCHMARK(BM_stdlib_atoi);
-
-void BM_stdlib_atol(benchmark::State& state) {
- for (auto _ : state) {
- benchmark::DoNotOptimize(atol(" -123"));
- }
-}
-BIONIC_BENCHMARK(BM_stdlib_atol);
-
-void BM_stdlib_strtol(benchmark::State& state) {
- for (auto _ : state) {
- benchmark::DoNotOptimize(strtol(" -123", nullptr, 0));
- }
-}
-BIONIC_BENCHMARK(BM_stdlib_strtol);
-
-void BM_stdlib_strtoll(benchmark::State& state) {
- for (auto _ : state) {
- benchmark::DoNotOptimize(strtoll(" -123", nullptr, 0));
- }
-}
-BIONIC_BENCHMARK(BM_stdlib_strtoll);
-
-void BM_stdlib_strtoul(benchmark::State& state) {
- for (auto _ : state) {
- benchmark::DoNotOptimize(strtoul(" -123", nullptr, 0));
- }
-}
-BIONIC_BENCHMARK(BM_stdlib_strtoul);
-
-void BM_stdlib_strtoull(benchmark::State& state) {
- for (auto _ : state) {
- benchmark::DoNotOptimize(strtoull(" -123", nullptr, 0));
- }
-}
-BIONIC_BENCHMARK(BM_stdlib_strtoull);
-
+BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atoi, atoi(" -123"));
+BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atol, atol(" -123"));
+BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol, strtol(" -123", nullptr, 0));
+BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoll, strtoll(" -123", nullptr, 0));
+BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul, strtoul(" -123", nullptr, 0));
+BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoull, strtoull(" -123", nullptr, 0));
diff --git a/benchmarks/unistd_benchmark.cpp b/benchmarks/unistd_benchmark.cpp
index 98e8858..d697dfd 100644
--- a/benchmarks/unistd_benchmark.cpp
+++ b/benchmarks/unistd_benchmark.cpp
@@ -20,37 +20,11 @@
#include <benchmark/benchmark.h>
#include "util.h"
-static void BM_unistd_getpid(benchmark::State& state) {
- while (state.KeepRunning()) {
- getpid();
- }
-}
-BIONIC_BENCHMARK(BM_unistd_getpid);
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid, getpid());
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid_syscall, syscall(__NR_getpid));
-static void BM_unistd_getpid_syscall(benchmark::State& state) {
- while (state.KeepRunning()) {
- syscall(__NR_getpid);
- }
-}
-BIONIC_BENCHMARK(BM_unistd_getpid_syscall);
-
+// TODO: glibc 2.30 added gettid() too.
#if defined(__BIONIC__)
-
-// Stop GCC optimizing out our pure function.
-/* Must not be static! */ pid_t (*gettid_fp)() = gettid;
-
-static void BM_unistd_gettid(benchmark::State& state) {
- while (state.KeepRunning()) {
- gettid_fp();
- }
-}
-BIONIC_BENCHMARK(BM_unistd_gettid);
-
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid, gettid());
#endif
-
-void BM_unistd_gettid_syscall(benchmark::State& state) {
- while (state.KeepRunning()) {
- syscall(__NR_gettid);
- }
-}
-BIONIC_BENCHMARK(BM_unistd_gettid_syscall);
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid_syscall, syscall(__NR_gettid));
diff --git a/benchmarks/util.h b/benchmarks/util.h
index 0813acc..ef4892d 100644
--- a/benchmarks/util.h
+++ b/benchmarks/util.h
@@ -41,6 +41,13 @@
#define BIONIC_BENCHMARK_WITH_ARG(n, arg) \
int _bionic_benchmark_##n __attribute__((unused)) = EmplaceBenchmark(std::string(#n), reinterpret_cast<benchmark_func_t>(n), arg)
+#define BIONIC_TRIVIAL_BENCHMARK(__name, __expression) \
+ static void __name(benchmark::State& state) { \
+ for (auto _ : state) { \
+ benchmark::DoNotOptimize(__expression); \
+ } \
+ } \
+ BIONIC_BENCHMARK(__name)
constexpr auto KB = 1024;
diff --git a/benchmarks/wctype_benchmark.cpp b/benchmarks/wctype_benchmark.cpp
index f030ddc..cdf5568 100644
--- a/benchmarks/wctype_benchmark.cpp
+++ b/benchmarks/wctype_benchmark.cpp
@@ -19,58 +19,14 @@
#include <benchmark/benchmark.h>
#include "util.h"
-static void BM_wctype_towlower_ascii_y(benchmark::State& state) {
- for (auto _ : state) {
- towlower('X');
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towlower_ascii_y);
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towlower_ascii_y, towlower('X'));
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towlower_ascii_n, towlower('x'));
-static void BM_wctype_towlower_ascii_n(benchmark::State& state) {
- for (auto _ : state) {
- towlower('x');
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towlower_ascii_n);
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towlower_unicode_y, towlower(0x0391));
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towlower_unicode_n, towlower(0x03b1));
-static void BM_wctype_towlower_unicode_y(benchmark::State& state) {
- for (auto _ : state) {
- towlower(0x0391);
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towlower_unicode_y);
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towupper_ascii_y, towupper('x'));
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towupper_ascii_n, towupper('X'));
-static void BM_wctype_towlower_unicode_n(benchmark::State& state) {
- for (auto _ : state) {
- towlower(0x03b1);
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towlower_unicode_n);
-
-static void BM_wctype_towupper_ascii_y(benchmark::State& state) {
- for (auto _ : state) {
- towupper('x');
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towupper_ascii_y);
-
-static void BM_wctype_towupper_ascii_n(benchmark::State& state) {
- for (auto _ : state) {
- towupper('X');
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towupper_ascii_n);
-
-static void BM_wctype_towupper_unicode_y(benchmark::State& state) {
- for (auto _ : state) {
- towupper(0x03b1);
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towupper_unicode_y);
-
-static void BM_wctype_towupper_unicode_n(benchmark::State& state) {
- for (auto _ : state) {
- towupper(0x0391);
- }
-}
-BIONIC_BENCHMARK(BM_wctype_towupper_unicode_n);
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towupper_unicode_y, towupper(0x03b1));
+BIONIC_TRIVIAL_BENCHMARK(BM_wctype_towupper_unicode_n, towupper(0x0391));