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