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" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 9 | #include "SkRefCnt.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 10 | #include "SkThread.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 | |
| 21 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 22 | return backend == kNonRendering_Backend; |
| 23 | } |
| 24 | |
| 25 | protected: |
| 26 | virtual const char* onGetName() { |
| 27 | return "atomic_inc_32"; |
| 28 | } |
| 29 | |
| 30 | virtual void onDraw(const int loops, SkCanvas*) { |
| 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 | |
| 45 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 46 | return backend == kNonRendering_Backend; |
| 47 | } |
| 48 | |
| 49 | protected: |
| 50 | virtual const char* onGetName() { |
| 51 | return "atomic_inc_64"; |
| 52 | } |
| 53 | |
| 54 | virtual void onDraw(const int loops, SkCanvas*) { |
| 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: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 67 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 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: |
| 72 | virtual const char* onGetName() { |
| 73 | return "ref_cnt_stack"; |
| 74 | } |
| 75 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 76 | virtual void onDraw(const int loops, SkCanvas*) { |
| 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: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 92 | SK_DECLARE_INST_COUNT(PlacedRefCnt) |
| 93 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 94 | PlacedRefCnt() : SkRefCnt() { } |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 95 | void operator delete(void*) { } |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | typedef SkRefCnt INHERITED; |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 101 | class RefCntBench_Heap : public Benchmark { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 102 | public: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 103 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 104 | return backend == kNonRendering_Backend; |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 105 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 106 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 107 | protected: |
| 108 | virtual const char* onGetName() { |
| 109 | return "ref_cnt_heap"; |
| 110 | } |
| 111 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 112 | virtual void onDraw(const int loops, SkCanvas*) { |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 113 | char memory[sizeof(PlacedRefCnt)]; |
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 | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 115 | PlacedRefCnt* ref = new (memory) PlacedRefCnt(); |
| 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 | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 128 | class RefCntBench_New : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 129 | public: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 130 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 131 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 132 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 133 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 134 | protected: |
| 135 | virtual const char* onGetName() { |
| 136 | return "ref_cnt_new"; |
| 137 | } |
| 138 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 139 | virtual void onDraw(const int loops, SkCanvas*) { |
| 140 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 141 | SkRefCnt* ref = new SkRefCnt(); |
| 142 | for (int j = 0; j < M; ++j) { |
| 143 | ref->ref(); |
| 144 | ref->unref(); |
| 145 | } |
| 146 | ref->unref(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 151 | typedef Benchmark INHERITED; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 154 | /////////////////////////////////////////////////////////////////////////////// |
| 155 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 156 | class WeakRefCntBench_Stack : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 157 | public: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 158 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 159 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 160 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 161 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 162 | protected: |
| 163 | virtual const char* onGetName() { |
| 164 | return "ref_cnt_stack_weak"; |
| 165 | } |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 166 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 167 | virtual void onDraw(const int loops, SkCanvas*) { |
| 168 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 169 | SkWeakRefCnt ref; |
| 170 | for (int j = 0; j < M; ++j) { |
| 171 | ref.ref(); |
| 172 | ref.unref(); |
| 173 | } |
| 174 | } |
| 175 | } |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 176 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 177 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 178 | typedef Benchmark INHERITED; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | class PlacedWeakRefCnt : public SkWeakRefCnt { |
| 182 | public: |
| 183 | PlacedWeakRefCnt() : SkWeakRefCnt() { } |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 184 | void operator delete(void*) { } |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 187 | class WeakRefCntBench_Heap : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 188 | public: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 189 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 190 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 191 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 192 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 193 | protected: |
| 194 | virtual const char* onGetName() { |
| 195 | return "ref_cnt_heap_weak"; |
| 196 | } |
| 197 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 198 | virtual void onDraw(const int loops, SkCanvas*) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 199 | char memory[sizeof(PlacedWeakRefCnt)]; |
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 | PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt(); |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 214 | class WeakRefCntBench_New : public Benchmark { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 215 | public: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 216 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 217 | return backend == kNonRendering_Backend; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 218 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 219 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 220 | protected: |
| 221 | virtual const char* onGetName() { |
| 222 | return "ref_cnt_new_weak"; |
| 223 | } |
| 224 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 225 | virtual void onDraw(const int loops, SkCanvas*) { |
| 226 | for (int i = 0; i < loops; ++i) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 227 | SkWeakRefCnt* ref = new SkWeakRefCnt(); |
| 228 | for (int j = 0; j < M; ++j) { |
| 229 | ref->ref(); |
| 230 | ref->unref(); |
| 231 | } |
| 232 | ref->unref(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 237 | typedef Benchmark INHERITED; |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | /////////////////////////////////////////////////////////////////////////////// |
| 241 | |
bsalomon | 00a8fae | 2014-07-11 08:42:11 -0700 | [diff] [blame] | 242 | DEF_BENCH( return new AtomicInc32(); ) |
| 243 | DEF_BENCH( return new AtomicInc64(); ) |
| 244 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 245 | DEF_BENCH( return new RefCntBench_Stack(); ) |
| 246 | DEF_BENCH( return new RefCntBench_Heap(); ) |
| 247 | DEF_BENCH( return new RefCntBench_New(); ) |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 248 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 249 | DEF_BENCH( return new WeakRefCntBench_Stack(); ) |
| 250 | DEF_BENCH( return new WeakRefCntBench_Heap(); ) |
| 251 | DEF_BENCH( return new WeakRefCntBench_New(); ) |