xref: /aosp_15_r20/external/pytorch/c10/benchmark/intrusive_ptr_benchmark.cpp (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #include <c10/util/intrusive_ptr.h>
2 #include <c10/util/irange.h>
3 
4 #include <benchmark/benchmark.h>
5 #include <memory>
6 
7 using c10::intrusive_ptr;
8 using c10::intrusive_ptr_target;
9 using c10::make_intrusive;
10 
11 namespace {
12 
13 // Foo uses intrusive ptr
14 class Foo : public intrusive_ptr_target {
15  public:
Foo(int param_)16   Foo(int param_) : param(param_) {}
17   int param;
18 };
19 
20 class Bar : public std::enable_shared_from_this<Bar> {
21  public:
Bar(int param_)22   Bar(int param_) : param(param_) {}
23   int param;
24 };
25 
BM_IntrusivePtrCtorDtor(benchmark::State & state)26 static void BM_IntrusivePtrCtorDtor(benchmark::State& state) {
27   intrusive_ptr<Foo> var = make_intrusive<Foo>(0);
28   while (state.KeepRunning()) {
29     // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
30     volatile intrusive_ptr<Foo> var2 = var;
31   }
32 }
33 BENCHMARK(BM_IntrusivePtrCtorDtor);
34 
BM_SharedPtrCtorDtor(benchmark::State & state)35 static void BM_SharedPtrCtorDtor(benchmark::State& state) {
36   std::shared_ptr<Bar> var = std::make_shared<Bar>(0);
37   while (state.KeepRunning()) {
38     // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
39     volatile std::shared_ptr<Bar> var2 = var;
40   }
41 }
42 BENCHMARK(BM_SharedPtrCtorDtor);
43 
BM_IntrusivePtrArray(benchmark::State & state)44 static void BM_IntrusivePtrArray(benchmark::State& state) {
45   intrusive_ptr<Foo> var = make_intrusive<Foo>(0);
46   const size_t kLength = state.range(0);
47   std::vector<intrusive_ptr<Foo>> vararray(kLength);
48   while (state.KeepRunning()) {
49     for (const auto i : c10::irange(kLength)) {
50       vararray[i] = var;
51     }
52     for (const auto i : c10::irange(kLength)) {
53       vararray[i].reset();
54     }
55   }
56 }
57 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-magic-numbers)
58 BENCHMARK(BM_IntrusivePtrArray)->RangeMultiplier(2)->Range(16, 4096);
59 
BM_SharedPtrArray(benchmark::State & state)60 static void BM_SharedPtrArray(benchmark::State& state) {
61   std::shared_ptr<Bar> var = std::make_shared<Bar>(0);
62   const size_t kLength = state.range(0);
63   std::vector<std::shared_ptr<Bar>> vararray(kLength);
64   while (state.KeepRunning()) {
65     for (const auto i : c10::irange(kLength)) {
66       vararray[i] = var;
67     }
68     for (const auto i : c10::irange(kLength)) {
69       vararray[i].reset();
70     }
71   }
72 }
73 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-magic-numbers)
74 BENCHMARK(BM_SharedPtrArray)->RangeMultiplier(2)->Range(16, 4096);
75 
BM_IntrusivePtrExclusiveOwnership(benchmark::State & state)76 static void BM_IntrusivePtrExclusiveOwnership(benchmark::State& state) {
77   while (state.KeepRunning()) {
78     volatile auto var = make_intrusive<Foo>(0);
79   }
80 }
81 BENCHMARK(BM_IntrusivePtrExclusiveOwnership);
82 
BM_SharedPtrExclusiveOwnership(benchmark::State & state)83 static void BM_SharedPtrExclusiveOwnership(benchmark::State& state) {
84   while (state.KeepRunning()) {
85     volatile auto var = std::make_shared<Foo>(0);
86   }
87 }
88 BENCHMARK(BM_SharedPtrExclusiveOwnership);
89 
90 } // namespace
91 
92 BENCHMARK_MAIN();
93