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