blob: f6aa3de942135483457df60fe6ea606ce08fffe2 [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 "Benchmark.h"
herb62a69c22015-09-29 11:47:45 -07008#include "SkAtomics.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +00009#include "SkRefCnt.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +000010#include "SkWeakRefCnt.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040011#include <memory>
12#include <new>
bungeman@google.com55487522012-05-14 14:09:24 +000013
14enum {
mtklein@google.comc2897432013-09-10 19:23:38 +000015 M = 2
bungeman@google.com55487522012-05-14 14:09:24 +000016};
17
bsalomon00a8fae2014-07-11 08:42:11 -070018class AtomicInc32 : public Benchmark {
19public:
20 AtomicInc32() : fX(0) {}
21
mtklein36352bf2015-03-25 18:17:31 -070022 bool isSuitableFor(Backend backend) override {
bsalomon00a8fae2014-07-11 08:42:11 -070023 return backend == kNonRendering_Backend;
24 }
25
26protected:
mtkleinf0599002015-07-13 06:18:39 -070027 const char* onGetName() override {
bsalomon00a8fae2014-07-11 08:42:11 -070028 return "atomic_inc_32";
29 }
30
mtkleina1ebeb22015-10-01 09:43:39 -070031 void onDraw(int loops, SkCanvas*) override {
bsalomon00a8fae2014-07-11 08:42:11 -070032 for (int i = 0; i < loops; ++i) {
33 sk_atomic_inc(&fX);
34 }
35 }
36
37private:
38 int32_t fX;
39 typedef Benchmark INHERITED;
40};
41
tfarinaf168b862014-06-19 12:32:29 -070042class RefCntBench_Stack : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000043public:
mtklein36352bf2015-03-25 18:17:31 -070044 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000045 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000046 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000047
bungeman@google.com55487522012-05-14 14:09:24 +000048protected:
mtkleinf0599002015-07-13 06:18:39 -070049 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +000050 return "ref_cnt_stack";
51 }
52
mtkleina1ebeb22015-10-01 09:43:39 -070053 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000054 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000055 SkRefCnt ref;
56 for (int j = 0; j < M; ++j) {
57 ref.ref();
58 ref.unref();
59 }
60 }
61 }
62
63private:
tfarinaf168b862014-06-19 12:32:29 -070064 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000065};
66
67class PlacedRefCnt : public SkRefCnt {
68public:
69 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000070 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000071
72private:
73 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000074};
75
tfarinaf168b862014-06-19 12:32:29 -070076class RefCntBench_Heap : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000077public:
mtklein36352bf2015-03-25 18:17:31 -070078 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000079 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000080 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000081
bungeman@google.com55487522012-05-14 14:09:24 +000082protected:
mtkleinf0599002015-07-13 06:18:39 -070083 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +000084 return "ref_cnt_heap";
85 }
86
mtkleina1ebeb22015-10-01 09:43:39 -070087 void onDraw(int loops, SkCanvas*) override {
bungeman@google.com55487522012-05-14 14:09:24 +000088 char memory[sizeof(PlacedRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +000089 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000090 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
91 for (int j = 0; j < M; ++j) {
92 ref->ref();
93 ref->unref();
94 }
95 ref->unref();
96 }
97 }
98
99private:
tfarinaf168b862014-06-19 12:32:29 -0700100 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +0000101};
102
tfarinaf168b862014-06-19 12:32:29 -0700103class RefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000104public:
mtklein36352bf2015-03-25 18:17:31 -0700105 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000106 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000107 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000108
bungeman@google.coma02bc152012-05-16 18:21:56 +0000109protected:
mtkleinf0599002015-07-13 06:18:39 -0700110 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000111 return "ref_cnt_new";
112 }
113
mtkleina1ebeb22015-10-01 09:43:39 -0700114 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000115 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000116 SkRefCnt* ref = new SkRefCnt();
117 for (int j = 0; j < M; ++j) {
118 ref->ref();
119 ref->unref();
120 }
121 ref->unref();
122 }
123 }
124
125private:
tfarinaf168b862014-06-19 12:32:29 -0700126 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000127};
128
bungeman@google.com55487522012-05-14 14:09:24 +0000129///////////////////////////////////////////////////////////////////////////////
130
tfarinaf168b862014-06-19 12:32:29 -0700131class WeakRefCntBench_Stack : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000132public:
mtklein36352bf2015-03-25 18:17:31 -0700133 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000134 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000135 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000136
bungeman@google.coma02bc152012-05-16 18:21:56 +0000137protected:
mtkleinf0599002015-07-13 06:18:39 -0700138 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000139 return "ref_cnt_stack_weak";
140 }
bungeman@google.com55487522012-05-14 14:09:24 +0000141
mtkleina1ebeb22015-10-01 09:43:39 -0700142 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000143 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000144 SkWeakRefCnt ref;
145 for (int j = 0; j < M; ++j) {
146 ref.ref();
147 ref.unref();
148 }
149 }
150 }
bungeman@google.com55487522012-05-14 14:09:24 +0000151
bungeman@google.coma02bc152012-05-16 18:21:56 +0000152private:
tfarinaf168b862014-06-19 12:32:29 -0700153 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000154};
155
156class PlacedWeakRefCnt : public SkWeakRefCnt {
157public:
158 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000159 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000160};
161
tfarinaf168b862014-06-19 12:32:29 -0700162class WeakRefCntBench_Heap : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000163public:
mtklein36352bf2015-03-25 18:17:31 -0700164 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000165 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000166 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000167
bungeman@google.coma02bc152012-05-16 18:21:56 +0000168protected:
mtklein36352bf2015-03-25 18:17:31 -0700169 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000170 return "ref_cnt_heap_weak";
171 }
172
mtkleina1ebeb22015-10-01 09:43:39 -0700173 void onDraw(int loops, SkCanvas*) override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000174 char memory[sizeof(PlacedWeakRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000175 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000176 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
177 for (int j = 0; j < M; ++j) {
178 ref->ref();
179 ref->unref();
180 }
181 ref->unref();
182 }
183 }
184
185private:
tfarinaf168b862014-06-19 12:32:29 -0700186 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000187};
188
tfarinaf168b862014-06-19 12:32:29 -0700189class WeakRefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000190public:
mtklein36352bf2015-03-25 18:17:31 -0700191 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000192 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000193 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000194
bungeman@google.coma02bc152012-05-16 18:21:56 +0000195protected:
mtklein36352bf2015-03-25 18:17:31 -0700196 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000197 return "ref_cnt_new_weak";
198 }
199
mtkleina1ebeb22015-10-01 09:43:39 -0700200 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000201 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000202 SkWeakRefCnt* ref = new SkWeakRefCnt();
203 for (int j = 0; j < M; ++j) {
204 ref->ref();
205 ref->unref();
206 }
207 ref->unref();
208 }
209 }
210
211private:
tfarinaf168b862014-06-19 12:32:29 -0700212 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000213};
214
215///////////////////////////////////////////////////////////////////////////////
216
bsalomon00a8fae2014-07-11 08:42:11 -0700217DEF_BENCH( return new AtomicInc32(); )
bsalomon00a8fae2014-07-11 08:42:11 -0700218
mtklein@google.com410e6e82013-09-13 19:52:27 +0000219DEF_BENCH( return new RefCntBench_Stack(); )
220DEF_BENCH( return new RefCntBench_Heap(); )
221DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000222
mtklein@google.com410e6e82013-09-13 19:52:27 +0000223DEF_BENCH( return new WeakRefCntBench_Stack(); )
224DEF_BENCH( return new WeakRefCntBench_Heap(); )
225DEF_BENCH( return new WeakRefCntBench_New(); )