1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "android-base/function_ref.h"
18
19 #include <benchmark/benchmark.h>
20
21 #include <functional>
22 #include <utility>
23
24 #include <time.h>
25
26 using android::base::function_ref;
27
28 template <class Callable, class... Args>
call(Callable && c,Args &&...args)29 [[clang::noinline]] auto call(Callable&& c, Args&&... args) {
30 return c(std::forward<Args>(args)...);
31 }
32
testFunc(int,const char *,char)33 [[clang::noinline]] static int testFunc(int, const char*, char) {
34 return time(nullptr);
35 }
36
37 using Func = decltype(testFunc);
38
BenchmarkFuncRaw(benchmark::State & state)39 static void BenchmarkFuncRaw(benchmark::State& state) {
40 for (auto _ : state) {
41 benchmark::DoNotOptimize(call(testFunc, 1, "1", '1'));
42 }
43 }
44 BENCHMARK(BenchmarkFuncRaw);
45
BenchmarkFuncPtr(benchmark::State & state)46 static void BenchmarkFuncPtr(benchmark::State& state) {
47 auto ptr = &testFunc;
48 for (auto _ : state) {
49 benchmark::DoNotOptimize(call(ptr, 1, "1", '1'));
50 }
51 }
52 BENCHMARK(BenchmarkFuncPtr);
53
BenchmarkStdFunction(benchmark::State & state)54 static void BenchmarkStdFunction(benchmark::State& state) {
55 std::function<Func> f(testFunc);
56 for (auto _ : state) {
57 benchmark::DoNotOptimize(call(f, 1, "1", '1'));
58 }
59 }
60 BENCHMARK(BenchmarkStdFunction);
61
BenchmarkFunctionRef(benchmark::State & state)62 static void BenchmarkFunctionRef(benchmark::State& state) {
63 function_ref<Func> f(testFunc);
64 for (auto _ : state) {
65 benchmark::DoNotOptimize(call(f, 1, "1", '1'));
66 }
67 }
68 BENCHMARK(BenchmarkFunctionRef);
69
70 namespace {
71 struct BigFunc {
72 char big[128];
operator ()__anonb68e18d10111::BigFunc73 [[clang::noinline]] int operator()(int, const char*, char) const { return time(nullptr); }
74 };
75
76 static BigFunc bigFunc;
77 } // namespace
78
BenchmarkBigRaw(benchmark::State & state)79 static void BenchmarkBigRaw(benchmark::State& state) {
80 for (auto _ : state) {
81 benchmark::DoNotOptimize(call(bigFunc, 1, "1", '1'));
82 }
83 }
84 BENCHMARK(BenchmarkBigRaw);
85
BenchmarkBigStdFunction(benchmark::State & state)86 static void BenchmarkBigStdFunction(benchmark::State& state) {
87 std::function<Func> f(bigFunc);
88 for (auto _ : state) {
89 benchmark::DoNotOptimize(call(f, 1, "1", '1'));
90 }
91 }
92 BENCHMARK(BenchmarkBigStdFunction);
93
BenchmarkBigFunctionRef(benchmark::State & state)94 static void BenchmarkBigFunctionRef(benchmark::State& state) {
95 function_ref<Func> f(bigFunc);
96 for (auto _ : state) {
97 benchmark::DoNotOptimize(call(f, 1, "1", '1'));
98 }
99 }
100 BENCHMARK(BenchmarkBigFunctionRef);
101
BenchmarkMakeFunctionRef(benchmark::State & state)102 static void BenchmarkMakeFunctionRef(benchmark::State& state) {
103 for (auto _ : state) {
104 benchmark::DoNotOptimize(call<function_ref<Func>>(bigFunc, 1, "1", '1'));
105 }
106 }
107 BENCHMARK(BenchmarkMakeFunctionRef);
108
BenchmarkMakeStdFunction(benchmark::State & state)109 static void BenchmarkMakeStdFunction(benchmark::State& state) {
110 for (auto _ : state) {
111 benchmark::DoNotOptimize(call<std::function<Func>>(bigFunc, 1, "1", '1'));
112 }
113 }
114 BENCHMARK(BenchmarkMakeStdFunction);
115