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 | */ |
| 7 | #include "SkBenchmark.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 8 | #include "SkRefCnt.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 9 | #include "SkThread.h" |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 10 | #include "SkWeakRefCnt.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 11 | #include <memory> |
| 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 | |
| 17 | class RefCntBench_Stack : public SkBenchmark { |
| 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: |
| 39 | typedef SkBenchmark INHERITED; |
| 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 | |
| 53 | class RefCntBench_Heap : public SkBenchmark { |
| 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: |
| 77 | typedef SkBenchmark INHERITED; |
| 78 | }; |
| 79 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 80 | class RefCntBench_New : public SkBenchmark { |
| 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: |
| 103 | typedef SkBenchmark INHERITED; |
| 104 | }; |
| 105 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 108 | class WeakRefCntBench_Stack : public SkBenchmark { |
| 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: |
| 130 | typedef SkBenchmark INHERITED; |
| 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 | |
| 139 | class WeakRefCntBench_Heap : public SkBenchmark { |
| 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: |
| 163 | typedef SkBenchmark INHERITED; |
| 164 | }; |
| 165 | |
| 166 | class WeakRefCntBench_New : public SkBenchmark { |
| 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: |
| 189 | typedef SkBenchmark INHERITED; |
| 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(); ) |