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