function_ref_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2019 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/functional/function_ref.h"
16 
17 #include <memory>
18 
19 #include "benchmark/benchmark.h"
20 #include "absl/base/attributes.h"
21 
22 namespace absl {
24 namespace {
25 
26 int dummy = 0;
27 
29 
30 struct TrivialFunctor {
32 };
33 
34 struct LargeFunctor {
35  void operator()() const { benchmark::DoNotOptimize(this); }
36  std::string a, b, c;
37 };
38 
39 template <typename Function, typename... Args>
41  f(std::forward<Args>(args)...);
42 }
43 
44 template <typename Function, typename Callable, typename... Args>
46  const Callable& c, Args&&... args) {
47  for (auto _ : state) {
48  CallFunction<Function>(c, std::forward<Args>(args)...);
49  }
50 }
51 
54  TrivialFunctor{});
55 }
57 
59  ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state,
60  TrivialFunctor{});
61 }
63 
66  LargeFunctor{});
67 }
69 
71  ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state, LargeFunctor{});
72 }
74 
77 }
79 
82 }
84 
85 // Doesn't include construction or copy overhead in the loop.
86 template <typename Function, typename Callable, typename... Args>
87 void CallFunctionBenchmark(benchmark::State& state, const Callable& c,
88  Args... args) {
89  Function f = c;
90  for (auto _ : state) {
92  f(args...);
93  }
94 }
95 
96 struct FunctorWithTrivialArgs {
97  void operator()(int a, int b, int c) const {
101  }
102 };
103 
105  CallFunctionBenchmark<std::function<void(int, int, int)>>(
106  state, FunctorWithTrivialArgs{}, 1, 2, 3);
107 }
109 
111  CallFunctionBenchmark<FunctionRef<void(int, int, int)>>(
112  state, FunctorWithTrivialArgs{}, 1, 2, 3);
113 }
115 
116 struct FunctorWithNonTrivialArgs {
121  }
122 };
123 
125  std::string a, b, c;
128  state, FunctorWithNonTrivialArgs{}, a, b, c);
129 }
131 
133  std::string a, b, c;
135  FunctionRef<void(std::string, std::string, std::string)>>(
136  state, FunctorWithNonTrivialArgs{}, a, b, c);
137 }
139 
140 } // namespace
142 } // namespace absl
absl::ABSL_NAMESPACE_BEGIN::ConstructAndCallFunctionBenchmark
void ConstructAndCallFunctionBenchmark(benchmark::State &state, const Callable &c, Args &&... args)
Definition: function_type_benchmark.cc:47
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
absl::ABSL_NAMESPACE_BEGIN::BM_LargeStdFunction
void BM_LargeStdFunction(benchmark::State &state)
Definition: function_type_benchmark.cc:72
absl::ABSL_NAMESPACE_BEGIN::FunctorWithTrivialArgs::operator()
void operator()(int a, int b, int c) const
Definition: function_ref_benchmark.cc:97
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::ABSL_NAMESPACE_BEGIN::FreeFunction
void FreeFunction()
Definition: function_type_benchmark.cc:30
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
absl::ABSL_NAMESPACE_BEGIN::BM_NonTrivialArgsFunctionRef
void BM_NonTrivialArgsFunctionRef(benchmark::State &state)
Definition: function_type_benchmark.cc:158
absl::ABSL_NAMESPACE_BEGIN::CallFunctionBenchmark
void CallFunctionBenchmark(benchmark::State &state, const Callable &c, Args... args)
Definition: function_type_benchmark.cc:107
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::ABSL_NAMESPACE_BEGIN::LargeFunctor::operator()
void operator()() const
Definition: function_ref_benchmark.cc:35
ABSL_ATTRIBUTE_NOINLINE
#define ABSL_ATTRIBUTE_NOINLINE
Definition: abseil-cpp/absl/base/attributes.h:112
absl::ABSL_NAMESPACE_BEGIN::BM_NonTrivialArgsStdFunction
void BM_NonTrivialArgsStdFunction(benchmark::State &state)
Definition: function_type_benchmark.cc:150
absl::ABSL_NAMESPACE_BEGIN::TrivialFunctor::operator()
void operator()() const
Definition: function_ref_benchmark.cc:31
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::ABSL_NAMESPACE_BEGIN::BM_TrivialFunctionRef
void BM_TrivialFunctionRef(benchmark::State &state)
Definition: function_type_benchmark.cc:60
hpack_encoder_fixtures::Args
Args({0, 16384})
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
benchmark::internal::Function
void() Function(State &)
Definition: benchmark/include/benchmark/benchmark.h:826
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
absl::ABSL_NAMESPACE_BEGIN::BM_FunPtrStdFunction
void BM_FunPtrStdFunction(benchmark::State &state)
Definition: function_type_benchmark.cc:90
absl::ABSL_NAMESPACE_BEGIN::BM_TrivialArgsStdFunction
void BM_TrivialArgsStdFunction(benchmark::State &state)
Definition: function_type_benchmark.cc:124
absl::ABSL_NAMESPACE_BEGIN::BM_FunPtrFunctionRef
void BM_FunPtrFunctionRef(benchmark::State &state)
Definition: function_type_benchmark.cc:95
absl::ABSL_NAMESPACE_BEGIN::BM_TrivialArgsFunctionRef
void BM_TrivialArgsFunctionRef(benchmark::State &state)
Definition: function_type_benchmark.cc:130
absl::ABSL_NAMESPACE_BEGIN::CallFunction
void ABSL_ATTRIBUTE_NOINLINE CallFunction(Function f, Args &&... args)
Definition: function_type_benchmark.cc:42
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
absl::ABSL_NAMESPACE_BEGIN::FunctorWithNonTrivialArgs::operator()
void operator()(std::string a, std::string b, std::string c) const
Definition: function_ref_benchmark.cc:117
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
absl::ABSL_NAMESPACE_BEGIN::dummy
int dummy
Definition: function_type_benchmark.cc:28
absl::ABSL_NAMESPACE_BEGIN::BM_TrivialStdFunction
void BM_TrivialStdFunction(benchmark::State &state)
Definition: function_type_benchmark.cc:54
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
BENCHMARK
#define BENCHMARK(n)
Definition: benchmark/include/benchmark/benchmark.h:1170
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
absl::ABSL_NAMESPACE_BEGIN::BM_LargeFunctionRef
void BM_LargeFunctionRef(benchmark::State &state)
Definition: function_type_benchmark.cc:78
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22