function_ref_benchmark.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "absl/functional/function_ref.h"
  15. #include <memory>
  16. #include "benchmark/benchmark.h"
  17. #include "absl/base/attributes.h"
  18. namespace absl {
  19. ABSL_NAMESPACE_BEGIN
  20. namespace {
  21. int dummy = 0;
  22. void FreeFunction() { benchmark::DoNotOptimize(dummy); }
  23. struct TrivialFunctor {
  24. void operator()() const { benchmark::DoNotOptimize(dummy); }
  25. };
  26. struct LargeFunctor {
  27. void operator()() const { benchmark::DoNotOptimize(this); }
  28. std::string a, b, c;
  29. };
  30. template <typename Function, typename... Args>
  31. void ABSL_ATTRIBUTE_NOINLINE CallFunction(Function f, Args&&... args) {
  32. f(std::forward<Args>(args)...);
  33. }
  34. template <typename Function, typename Callable, typename... Args>
  35. void ConstructAndCallFunctionBenchmark(benchmark::State& state,
  36. const Callable& c, Args&&... args) {
  37. for (auto _ : state) {
  38. CallFunction<Function>(c, std::forward<Args>(args)...);
  39. }
  40. }
  41. void BM_TrivialStdFunction(benchmark::State& state) {
  42. ConstructAndCallFunctionBenchmark<std::function<void()>>(state,
  43. TrivialFunctor{});
  44. }
  45. BENCHMARK(BM_TrivialStdFunction);
  46. void BM_TrivialFunctionRef(benchmark::State& state) {
  47. ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state,
  48. TrivialFunctor{});
  49. }
  50. BENCHMARK(BM_TrivialFunctionRef);
  51. void BM_LargeStdFunction(benchmark::State& state) {
  52. ConstructAndCallFunctionBenchmark<std::function<void()>>(state,
  53. LargeFunctor{});
  54. }
  55. BENCHMARK(BM_LargeStdFunction);
  56. void BM_LargeFunctionRef(benchmark::State& state) {
  57. ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state, LargeFunctor{});
  58. }
  59. BENCHMARK(BM_LargeFunctionRef);
  60. void BM_FunPtrStdFunction(benchmark::State& state) {
  61. ConstructAndCallFunctionBenchmark<std::function<void()>>(state, FreeFunction);
  62. }
  63. BENCHMARK(BM_FunPtrStdFunction);
  64. void BM_FunPtrFunctionRef(benchmark::State& state) {
  65. ConstructAndCallFunctionBenchmark<FunctionRef<void()>>(state, FreeFunction);
  66. }
  67. BENCHMARK(BM_FunPtrFunctionRef);
  68. // Doesn't include construction or copy overhead in the loop.
  69. template <typename Function, typename Callable, typename... Args>
  70. void CallFunctionBenchmark(benchmark::State& state, const Callable& c,
  71. Args... args) {
  72. Function f = c;
  73. for (auto _ : state) {
  74. benchmark::DoNotOptimize(&f);
  75. f(args...);
  76. }
  77. }
  78. struct FunctorWithTrivialArgs {
  79. void operator()(int a, int b, int c) const {
  80. benchmark::DoNotOptimize(a);
  81. benchmark::DoNotOptimize(b);
  82. benchmark::DoNotOptimize(c);
  83. }
  84. };
  85. void BM_TrivialArgsStdFunction(benchmark::State& state) {
  86. CallFunctionBenchmark<std::function<void(int, int, int)>>(
  87. state, FunctorWithTrivialArgs{}, 1, 2, 3);
  88. }
  89. BENCHMARK(BM_TrivialArgsStdFunction);
  90. void BM_TrivialArgsFunctionRef(benchmark::State& state) {
  91. CallFunctionBenchmark<FunctionRef<void(int, int, int)>>(
  92. state, FunctorWithTrivialArgs{}, 1, 2, 3);
  93. }
  94. BENCHMARK(BM_TrivialArgsFunctionRef);
  95. struct FunctorWithNonTrivialArgs {
  96. void operator()(std::string a, std::string b, std::string c) const {
  97. benchmark::DoNotOptimize(&a);
  98. benchmark::DoNotOptimize(&b);
  99. benchmark::DoNotOptimize(&c);
  100. }
  101. };
  102. void BM_NonTrivialArgsStdFunction(benchmark::State& state) {
  103. std::string a, b, c;
  104. CallFunctionBenchmark<
  105. std::function<void(std::string, std::string, std::string)>>(
  106. state, FunctorWithNonTrivialArgs{}, a, b, c);
  107. }
  108. BENCHMARK(BM_NonTrivialArgsStdFunction);
  109. void BM_NonTrivialArgsFunctionRef(benchmark::State& state) {
  110. std::string a, b, c;
  111. CallFunctionBenchmark<
  112. FunctionRef<void(std::string, std::string, std::string)>>(
  113. state, FunctorWithNonTrivialArgs{}, a, b, c);
  114. }
  115. BENCHMARK(BM_NonTrivialArgsFunctionRef);
  116. } // namespace
  117. ABSL_NAMESPACE_END
  118. } // namespace absl