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