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