blob: aacdf18edb83dc4efef9c4766ae659884789bd0b [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"
bungeman@google.coma02bc152012-05-16 18:21:56 +00009#include "SkRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +000010#include "SkThread.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:
26 virtual const char* onGetName() {
27 return "atomic_inc_32";
28 }
29
30 virtual void onDraw(const int loops, SkCanvas*) {
31 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
41class AtomicInc64 : public Benchmark {
42public:
43 AtomicInc64() : fX(0) {}
44
mtklein36352bf2015-03-25 18:17:31 -070045 bool isSuitableFor(Backend backend) override {
bsalomon00a8fae2014-07-11 08:42:11 -070046 return backend == kNonRendering_Backend;
47 }
48
49protected:
50 virtual const char* onGetName() {
51 return "atomic_inc_64";
52 }
53
54 virtual void onDraw(const int loops, SkCanvas*) {
55 for (int i = 0; i < loops; ++i) {
56 sk_atomic_inc(&fX);
57 }
58 }
59
60private:
61 int64_t fX;
62 typedef Benchmark INHERITED;
63};
64
tfarinaf168b862014-06-19 12:32:29 -070065class RefCntBench_Stack : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000066public:
mtklein36352bf2015-03-25 18:17:31 -070067 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000068 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000069 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000070
bungeman@google.com55487522012-05-14 14:09:24 +000071protected:
72 virtual const char* onGetName() {
73 return "ref_cnt_stack";
74 }
75
commit-bot@chromium.org33614712013-12-03 18:17:16 +000076 virtual void onDraw(const int loops, SkCanvas*) {
77 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000078 SkRefCnt ref;
79 for (int j = 0; j < M; ++j) {
80 ref.ref();
81 ref.unref();
82 }
83 }
84 }
85
86private:
tfarinaf168b862014-06-19 12:32:29 -070087 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000088};
89
90class PlacedRefCnt : public SkRefCnt {
91public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000092 SK_DECLARE_INST_COUNT(PlacedRefCnt)
93
bungeman@google.com55487522012-05-14 14:09:24 +000094 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000095 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000096
97private:
98 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000099};
100
tfarinaf168b862014-06-19 12:32:29 -0700101class RefCntBench_Heap : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +0000102public:
mtklein36352bf2015-03-25 18:17:31 -0700103 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000104 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +0000105 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000106
bungeman@google.com55487522012-05-14 14:09:24 +0000107protected:
108 virtual const char* onGetName() {
109 return "ref_cnt_heap";
110 }
111
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000112 virtual void onDraw(const int loops, SkCanvas*) {
bungeman@google.com55487522012-05-14 14:09:24 +0000113 char memory[sizeof(PlacedRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000114 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +0000115 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
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.com55487522012-05-14 14:09:24 +0000126};
127
tfarinaf168b862014-06-19 12:32:29 -0700128class RefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000129public:
mtklein36352bf2015-03-25 18:17:31 -0700130 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000131 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000132 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000133
bungeman@google.coma02bc152012-05-16 18:21:56 +0000134protected:
135 virtual const char* onGetName() {
136 return "ref_cnt_new";
137 }
138
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000139 virtual void onDraw(const int loops, SkCanvas*) {
140 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000141 SkRefCnt* ref = new SkRefCnt();
142 for (int j = 0; j < M; ++j) {
143 ref->ref();
144 ref->unref();
145 }
146 ref->unref();
147 }
148 }
149
150private:
tfarinaf168b862014-06-19 12:32:29 -0700151 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000152};
153
bungeman@google.com55487522012-05-14 14:09:24 +0000154///////////////////////////////////////////////////////////////////////////////
155
tfarinaf168b862014-06-19 12:32:29 -0700156class WeakRefCntBench_Stack : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000157public:
mtklein36352bf2015-03-25 18:17:31 -0700158 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000159 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000160 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000161
bungeman@google.coma02bc152012-05-16 18:21:56 +0000162protected:
163 virtual const char* onGetName() {
164 return "ref_cnt_stack_weak";
165 }
bungeman@google.com55487522012-05-14 14:09:24 +0000166
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000167 virtual void onDraw(const int loops, SkCanvas*) {
168 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000169 SkWeakRefCnt ref;
170 for (int j = 0; j < M; ++j) {
171 ref.ref();
172 ref.unref();
173 }
174 }
175 }
bungeman@google.com55487522012-05-14 14:09:24 +0000176
bungeman@google.coma02bc152012-05-16 18:21:56 +0000177private:
tfarinaf168b862014-06-19 12:32:29 -0700178 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000179};
180
181class PlacedWeakRefCnt : public SkWeakRefCnt {
182public:
183 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000184 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000185};
186
tfarinaf168b862014-06-19 12:32:29 -0700187class WeakRefCntBench_Heap : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000188public:
mtklein36352bf2015-03-25 18:17:31 -0700189 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000190 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000191 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000192
bungeman@google.coma02bc152012-05-16 18:21:56 +0000193protected:
mtklein36352bf2015-03-25 18:17:31 -0700194 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000195 return "ref_cnt_heap_weak";
196 }
197
mtklein36352bf2015-03-25 18:17:31 -0700198 void onDraw(const int loops, SkCanvas*) override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000199 char memory[sizeof(PlacedWeakRefCnt)];
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 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
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
tfarinaf168b862014-06-19 12:32:29 -0700214class WeakRefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000215public:
mtklein36352bf2015-03-25 18:17:31 -0700216 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000217 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000218 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000219
bungeman@google.coma02bc152012-05-16 18:21:56 +0000220protected:
mtklein36352bf2015-03-25 18:17:31 -0700221 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000222 return "ref_cnt_new_weak";
223 }
224
mtklein36352bf2015-03-25 18:17:31 -0700225 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000226 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000227 SkWeakRefCnt* ref = new SkWeakRefCnt();
228 for (int j = 0; j < M; ++j) {
229 ref->ref();
230 ref->unref();
231 }
232 ref->unref();
233 }
234 }
235
236private:
tfarinaf168b862014-06-19 12:32:29 -0700237 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000238};
239
240///////////////////////////////////////////////////////////////////////////////
241
bsalomon00a8fae2014-07-11 08:42:11 -0700242DEF_BENCH( return new AtomicInc32(); )
243DEF_BENCH( return new AtomicInc64(); )
244
mtklein@google.com410e6e82013-09-13 19:52:27 +0000245DEF_BENCH( return new RefCntBench_Stack(); )
246DEF_BENCH( return new RefCntBench_Heap(); )
247DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000248
mtklein@google.com410e6e82013-09-13 19:52:27 +0000249DEF_BENCH( return new WeakRefCntBench_Stack(); )
250DEF_BENCH( return new WeakRefCntBench_Heap(); )
251DEF_BENCH( return new WeakRefCntBench_New(); )