blob: 5e9e3a096025bbad2525838c0226e36cc5e032f1 [file] [log] [blame]
bungeman@google.com55487522012-05-14 14:09:24 +00001/*
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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include <memory>
8#include "Benchmark.h"
herb62a69c22015-09-29 11:47:45 -07009#include "SkAtomics.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +000010#include "SkRefCnt.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +000011#include "SkWeakRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +000012
13enum {
mtklein@google.comc2897432013-09-10 19:23:38 +000014 M = 2
bungeman@google.com55487522012-05-14 14:09:24 +000015};
16
bsalomon00a8fae2014-07-11 08:42:11 -070017class AtomicInc32 : public Benchmark {
18public:
19 AtomicInc32() : fX(0) {}
20
mtklein36352bf2015-03-25 18:17:31 -070021 bool isSuitableFor(Backend backend) override {
bsalomon00a8fae2014-07-11 08:42:11 -070022 return backend == kNonRendering_Backend;
23 }
24
25protected:
mtkleinf0599002015-07-13 06:18:39 -070026 const char* onGetName() override {
bsalomon00a8fae2014-07-11 08:42:11 -070027 return "atomic_inc_32";
28 }
29
mtkleina1ebeb22015-10-01 09:43:39 -070030 void onDraw(int loops, SkCanvas*) override {
bsalomon00a8fae2014-07-11 08:42:11 -070031 for (int i = 0; i < loops; ++i) {
32 sk_atomic_inc(&fX);
33 }
34 }
35
36private:
37 int32_t fX;
38 typedef Benchmark INHERITED;
39};
40
tfarinaf168b862014-06-19 12:32:29 -070041class RefCntBench_Stack : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000042public:
mtklein36352bf2015-03-25 18:17:31 -070043 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000044 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000045 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000046
bungeman@google.com55487522012-05-14 14:09:24 +000047protected:
mtkleinf0599002015-07-13 06:18:39 -070048 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +000049 return "ref_cnt_stack";
50 }
51
mtkleina1ebeb22015-10-01 09:43:39 -070052 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000053 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000054 SkRefCnt ref;
55 for (int j = 0; j < M; ++j) {
56 ref.ref();
57 ref.unref();
58 }
59 }
60 }
61
62private:
tfarinaf168b862014-06-19 12:32:29 -070063 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000064};
65
66class PlacedRefCnt : public SkRefCnt {
67public:
68 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000069 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000070
71private:
72 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000073};
74
tfarinaf168b862014-06-19 12:32:29 -070075class RefCntBench_Heap : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000076public:
mtklein36352bf2015-03-25 18:17:31 -070077 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000078 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000079 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000080
bungeman@google.com55487522012-05-14 14:09:24 +000081protected:
mtkleinf0599002015-07-13 06:18:39 -070082 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +000083 return "ref_cnt_heap";
84 }
85
mtkleina1ebeb22015-10-01 09:43:39 -070086 void onDraw(int loops, SkCanvas*) override {
bungeman@google.com55487522012-05-14 14:09:24 +000087 char memory[sizeof(PlacedRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +000088 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000089 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
90 for (int j = 0; j < M; ++j) {
91 ref->ref();
92 ref->unref();
93 }
94 ref->unref();
95 }
96 }
97
98private:
tfarinaf168b862014-06-19 12:32:29 -070099 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +0000100};
101
tfarinaf168b862014-06-19 12:32:29 -0700102class RefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000103public:
mtklein36352bf2015-03-25 18:17:31 -0700104 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000105 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000106 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000107
bungeman@google.coma02bc152012-05-16 18:21:56 +0000108protected:
mtkleinf0599002015-07-13 06:18:39 -0700109 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000110 return "ref_cnt_new";
111 }
112
mtkleina1ebeb22015-10-01 09:43:39 -0700113 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000114 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000115 SkRefCnt* ref = new SkRefCnt();
116 for (int j = 0; j < M; ++j) {
117 ref->ref();
118 ref->unref();
119 }
120 ref->unref();
121 }
122 }
123
124private:
tfarinaf168b862014-06-19 12:32:29 -0700125 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000126};
127
bungeman@google.com55487522012-05-14 14:09:24 +0000128///////////////////////////////////////////////////////////////////////////////
129
tfarinaf168b862014-06-19 12:32:29 -0700130class WeakRefCntBench_Stack : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000131public:
mtklein36352bf2015-03-25 18:17:31 -0700132 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000133 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000134 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000135
bungeman@google.coma02bc152012-05-16 18:21:56 +0000136protected:
mtkleinf0599002015-07-13 06:18:39 -0700137 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000138 return "ref_cnt_stack_weak";
139 }
bungeman@google.com55487522012-05-14 14:09:24 +0000140
mtkleina1ebeb22015-10-01 09:43:39 -0700141 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000142 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000143 SkWeakRefCnt ref;
144 for (int j = 0; j < M; ++j) {
145 ref.ref();
146 ref.unref();
147 }
148 }
149 }
bungeman@google.com55487522012-05-14 14:09:24 +0000150
bungeman@google.coma02bc152012-05-16 18:21:56 +0000151private:
tfarinaf168b862014-06-19 12:32:29 -0700152 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000153};
154
155class PlacedWeakRefCnt : public SkWeakRefCnt {
156public:
157 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000158 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000159};
160
tfarinaf168b862014-06-19 12:32:29 -0700161class WeakRefCntBench_Heap : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000162public:
mtklein36352bf2015-03-25 18:17:31 -0700163 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000164 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000165 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000166
bungeman@google.coma02bc152012-05-16 18:21:56 +0000167protected:
mtklein36352bf2015-03-25 18:17:31 -0700168 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000169 return "ref_cnt_heap_weak";
170 }
171
mtkleina1ebeb22015-10-01 09:43:39 -0700172 void onDraw(int loops, SkCanvas*) override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000173 char memory[sizeof(PlacedWeakRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000174 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000175 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
176 for (int j = 0; j < M; ++j) {
177 ref->ref();
178 ref->unref();
179 }
180 ref->unref();
181 }
182 }
183
184private:
tfarinaf168b862014-06-19 12:32:29 -0700185 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000186};
187
tfarinaf168b862014-06-19 12:32:29 -0700188class WeakRefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000189public:
mtklein36352bf2015-03-25 18:17:31 -0700190 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000191 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000192 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000193
bungeman@google.coma02bc152012-05-16 18:21:56 +0000194protected:
mtklein36352bf2015-03-25 18:17:31 -0700195 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000196 return "ref_cnt_new_weak";
197 }
198
mtkleina1ebeb22015-10-01 09:43:39 -0700199 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000200 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000201 SkWeakRefCnt* ref = new SkWeakRefCnt();
202 for (int j = 0; j < M; ++j) {
203 ref->ref();
204 ref->unref();
205 }
206 ref->unref();
207 }
208 }
209
210private:
tfarinaf168b862014-06-19 12:32:29 -0700211 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000212};
213
214///////////////////////////////////////////////////////////////////////////////
215
bsalomon00a8fae2014-07-11 08:42:11 -0700216DEF_BENCH( return new AtomicInc32(); )
bsalomon00a8fae2014-07-11 08:42:11 -0700217
mtklein@google.com410e6e82013-09-13 19:52:27 +0000218DEF_BENCH( return new RefCntBench_Stack(); )
219DEF_BENCH( return new RefCntBench_Heap(); )
220DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000221
mtklein@google.com410e6e82013-09-13 19:52:27 +0000222DEF_BENCH( return new WeakRefCntBench_Stack(); )
223DEF_BENCH( return new WeakRefCntBench_Heap(); )
224DEF_BENCH( return new WeakRefCntBench_New(); )