bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 1 | /* |
2 | * Copyright 2012 Google Inc. | ||||
3 | * | ||||
4 | * Use of this source code is governed by a BSD-style license that can be | ||||
5 | * found in the LICENSE file. | ||||
6 | */ | ||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
herb | 62a69c2 | 2015-09-29 11:47:45 -0700 | [diff] [blame] | 8 | #include "SkAtomics.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 9 | #include "SkRefCnt.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 10 | #include "SkWeakRefCnt.h" |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 11 | #include <memory> |
12 | #include <new> | ||||
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 13 | |
14 | enum { | ||||
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 15 | M = 2 |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 16 | }; |
17 | |||||
bsalomon | 00a8fae | 2014-07-11 08:42:11 -0700 | [diff] [blame] | 18 | class AtomicInc32 : public Benchmark { |
19 | public: | ||||
20 | AtomicInc32() : fX(0) {} | ||||
21 | |||||
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 22 | bool isSuitableFor(Backend backend) override { |
bsalomon | 00a8fae | 2014-07-11 08:42:11 -0700 | [diff] [blame] | 23 | return backend == kNonRendering_Backend; |
24 | } | ||||
25 | |||||
26 | protected: | ||||
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 27 | const char* onGetName() override { |
bsalomon | 00a8fae | 2014-07-11 08:42:11 -0700 | [diff] [blame] | 28 | return "atomic_inc_32"; |
29 | } | ||||
30 | |||||
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 31 | void onDraw(int loops, SkCanvas*) override { |
bsalomon | 00a8fae | 2014-07-11 08:42:11 -0700 | [diff] [blame] | 32 | for (int i = 0; i < loops; ++i) { |
33 | sk_atomic_inc(&fX); | ||||
34 | } | ||||
35 | } | ||||
36 | |||||
37 | private: | ||||
38 | int32_t fX; | ||||
39 | typedef Benchmark INHERITED; | ||||
40 | }; | ||||
41 | |||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 42 | class RefCntBench_Stack : public Benchmark { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 43 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 45 | return backend == kNonRendering_Backend; |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 46 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 47 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 48 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 49 | const char* onGetName() override { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 50 | return "ref_cnt_stack"; |
51 | } | ||||
52 | |||||
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 53 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 54 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 55 | SkRefCnt ref; |
56 | for (int j = 0; j < M; ++j) { | ||||
57 | ref.ref(); | ||||
58 | ref.unref(); | ||||
59 | } | ||||
60 | } | ||||
61 | } | ||||
62 | |||||
63 | private: | ||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 64 | typedef Benchmark INHERITED; |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 65 | }; |
66 | |||||
67 | class PlacedRefCnt : public SkRefCnt { | ||||
68 | public: | ||||
69 | PlacedRefCnt() : SkRefCnt() { } | ||||
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 70 | void operator delete(void*) { } |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 71 | |
72 | private: | ||||
73 | typedef SkRefCnt INHERITED; | ||||
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 74 | }; |
75 | |||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 76 | class RefCntBench_Heap : public Benchmark { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 77 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 79 | return backend == kNonRendering_Backend; |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 80 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 81 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 82 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 83 | const char* onGetName() override { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 84 | return "ref_cnt_heap"; |
85 | } | ||||
86 | |||||
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 87 | void onDraw(int loops, SkCanvas*) override { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 88 | char memory[sizeof(PlacedRefCnt)]; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 89 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 90 | PlacedRefCnt* ref = new (memory) PlacedRefCnt(); |
91 | for (int j = 0; j < M; ++j) { | ||||
92 | ref->ref(); | ||||
93 | ref->unref(); | ||||
94 | } | ||||
95 | ref->unref(); | ||||
96 | } | ||||
97 | } | ||||
98 | |||||
99 | private: | ||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 100 | typedef Benchmark INHERITED; |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 101 | }; |
102 | |||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 103 | class RefCntBench_New : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 104 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 105 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 106 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 107 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 108 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 109 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 110 | const char* onGetName() override { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 111 | return "ref_cnt_new"; |
112 | } | ||||
113 | |||||
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 114 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 115 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 116 | SkRefCnt* ref = new SkRefCnt(); |
117 | for (int j = 0; j < M; ++j) { | ||||
118 | ref->ref(); | ||||
119 | ref->unref(); | ||||
120 | } | ||||
121 | ref->unref(); | ||||
122 | } | ||||
123 | } | ||||
124 | |||||
125 | private: | ||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 126 | typedef Benchmark INHERITED; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 127 | }; |
128 | |||||
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////// |
130 | |||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 131 | class WeakRefCntBench_Stack : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 132 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 133 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 134 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 135 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 136 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 137 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 138 | const char* onGetName() override { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 139 | return "ref_cnt_stack_weak"; |
140 | } | ||||
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 141 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 142 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 143 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 144 | SkWeakRefCnt ref; |
145 | for (int j = 0; j < M; ++j) { | ||||
146 | ref.ref(); | ||||
147 | ref.unref(); | ||||
148 | } | ||||
149 | } | ||||
150 | } | ||||
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 151 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 152 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 153 | typedef Benchmark INHERITED; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 154 | }; |
155 | |||||
156 | class PlacedWeakRefCnt : public SkWeakRefCnt { | ||||
157 | public: | ||||
158 | PlacedWeakRefCnt() : SkWeakRefCnt() { } | ||||
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 159 | void operator delete(void*) { } |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 160 | }; |
161 | |||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 162 | class WeakRefCntBench_Heap : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 163 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 164 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 165 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 166 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 167 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 168 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 169 | const char* onGetName() override { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 170 | return "ref_cnt_heap_weak"; |
171 | } | ||||
172 | |||||
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 173 | void onDraw(int loops, SkCanvas*) override { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 174 | char memory[sizeof(PlacedWeakRefCnt)]; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 175 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 176 | PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt(); |
177 | for (int j = 0; j < M; ++j) { | ||||
178 | ref->ref(); | ||||
179 | ref->unref(); | ||||
180 | } | ||||
181 | ref->unref(); | ||||
182 | } | ||||
183 | } | ||||
184 | |||||
185 | private: | ||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 186 | typedef Benchmark INHERITED; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 187 | }; |
188 | |||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 189 | class WeakRefCntBench_New : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 190 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 191 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 192 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 193 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 194 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 195 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 196 | const char* onGetName() override { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 197 | return "ref_cnt_new_weak"; |
198 | } | ||||
199 | |||||
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 200 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 201 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 202 | SkWeakRefCnt* ref = new SkWeakRefCnt(); |
203 | for (int j = 0; j < M; ++j) { | ||||
204 | ref->ref(); | ||||
205 | ref->unref(); | ||||
206 | } | ||||
207 | ref->unref(); | ||||
208 | } | ||||
209 | } | ||||
210 | |||||
211 | private: | ||||
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 212 | typedef Benchmark INHERITED; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 213 | }; |
214 | |||||
215 | /////////////////////////////////////////////////////////////////////////////// | ||||
216 | |||||
bsalomon | 00a8fae | 2014-07-11 08:42:11 -0700 | [diff] [blame] | 217 | DEF_BENCH( return new AtomicInc32(); ) |
bsalomon | 00a8fae | 2014-07-11 08:42:11 -0700 | [diff] [blame] | 218 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 219 | DEF_BENCH( return new RefCntBench_Stack(); ) |
220 | DEF_BENCH( return new RefCntBench_Heap(); ) | ||||
221 | DEF_BENCH( return new RefCntBench_New(); ) | ||||
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 222 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 223 | DEF_BENCH( return new WeakRefCntBench_Stack(); ) |
224 | DEF_BENCH( return new WeakRefCntBench_Heap(); ) | ||||
225 | DEF_BENCH( return new WeakRefCntBench_New(); ) |