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