blob: 313e9a484b13e9e26d88dd636b766e5a92752fe0 [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"
mtklein1b249332015-07-07 12:21:21 -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
mtkleinf0599002015-07-13 06:18:39 -070030 void onDraw(const 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
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:
mtkleinf0599002015-07-13 06:18:39 -070050 const char* onGetName() override {
bsalomon00a8fae2014-07-11 08:42:11 -070051 return "atomic_inc_64";
52 }
53
mtkleinf0599002015-07-13 06:18:39 -070054 void onDraw(const int loops, SkCanvas*) override {
bsalomon00a8fae2014-07-11 08:42:11 -070055 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:
mtkleinf0599002015-07-13 06:18:39 -070072 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +000073 return "ref_cnt_stack";
74 }
75
mtkleinf0599002015-07-13 06:18:39 -070076 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000077 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:
92 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000093 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000094
95private:
96 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000097};
98
tfarinaf168b862014-06-19 12:32:29 -070099class RefCntBench_Heap : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +0000100public:
mtklein36352bf2015-03-25 18:17:31 -0700101 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000102 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +0000103 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000104
bungeman@google.com55487522012-05-14 14:09:24 +0000105protected:
mtkleinf0599002015-07-13 06:18:39 -0700106 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +0000107 return "ref_cnt_heap";
108 }
109
mtkleinf0599002015-07-13 06:18:39 -0700110 void onDraw(const int loops, SkCanvas*) override {
bungeman@google.com55487522012-05-14 14:09:24 +0000111 char memory[sizeof(PlacedRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000112 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +0000113 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
114 for (int j = 0; j < M; ++j) {
115 ref->ref();
116 ref->unref();
117 }
118 ref->unref();
119 }
120 }
121
122private:
tfarinaf168b862014-06-19 12:32:29 -0700123 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +0000124};
125
tfarinaf168b862014-06-19 12:32:29 -0700126class RefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000127public:
mtklein36352bf2015-03-25 18:17:31 -0700128 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000129 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000130 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000131
bungeman@google.coma02bc152012-05-16 18:21:56 +0000132protected:
mtkleinf0599002015-07-13 06:18:39 -0700133 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000134 return "ref_cnt_new";
135 }
136
mtkleinf0599002015-07-13 06:18:39 -0700137 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000138 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000139 SkRefCnt* ref = new SkRefCnt();
140 for (int j = 0; j < M; ++j) {
141 ref->ref();
142 ref->unref();
143 }
144 ref->unref();
145 }
146 }
147
148private:
tfarinaf168b862014-06-19 12:32:29 -0700149 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000150};
151
bungeman@google.com55487522012-05-14 14:09:24 +0000152///////////////////////////////////////////////////////////////////////////////
153
tfarinaf168b862014-06-19 12:32:29 -0700154class WeakRefCntBench_Stack : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000155public:
mtklein36352bf2015-03-25 18:17:31 -0700156 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000157 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000158 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000159
bungeman@google.coma02bc152012-05-16 18:21:56 +0000160protected:
mtkleinf0599002015-07-13 06:18:39 -0700161 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000162 return "ref_cnt_stack_weak";
163 }
bungeman@google.com55487522012-05-14 14:09:24 +0000164
mtkleinf0599002015-07-13 06:18:39 -0700165 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000166 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000167 SkWeakRefCnt ref;
168 for (int j = 0; j < M; ++j) {
169 ref.ref();
170 ref.unref();
171 }
172 }
173 }
bungeman@google.com55487522012-05-14 14:09:24 +0000174
bungeman@google.coma02bc152012-05-16 18:21:56 +0000175private:
tfarinaf168b862014-06-19 12:32:29 -0700176 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000177};
178
179class PlacedWeakRefCnt : public SkWeakRefCnt {
180public:
181 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000182 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000183};
184
tfarinaf168b862014-06-19 12:32:29 -0700185class WeakRefCntBench_Heap : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000186public:
mtklein36352bf2015-03-25 18:17:31 -0700187 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000188 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000189 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000190
bungeman@google.coma02bc152012-05-16 18:21:56 +0000191protected:
mtklein36352bf2015-03-25 18:17:31 -0700192 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000193 return "ref_cnt_heap_weak";
194 }
195
mtklein36352bf2015-03-25 18:17:31 -0700196 void onDraw(const int loops, SkCanvas*) override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000197 char memory[sizeof(PlacedWeakRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000198 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000199 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
200 for (int j = 0; j < M; ++j) {
201 ref->ref();
202 ref->unref();
203 }
204 ref->unref();
205 }
206 }
207
208private:
tfarinaf168b862014-06-19 12:32:29 -0700209 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000210};
211
tfarinaf168b862014-06-19 12:32:29 -0700212class WeakRefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000213public:
mtklein36352bf2015-03-25 18:17:31 -0700214 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000215 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000216 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000217
bungeman@google.coma02bc152012-05-16 18:21:56 +0000218protected:
mtklein36352bf2015-03-25 18:17:31 -0700219 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000220 return "ref_cnt_new_weak";
221 }
222
mtklein36352bf2015-03-25 18:17:31 -0700223 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000224 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000225 SkWeakRefCnt* ref = new SkWeakRefCnt();
226 for (int j = 0; j < M; ++j) {
227 ref->ref();
228 ref->unref();
229 }
230 ref->unref();
231 }
232 }
233
234private:
tfarinaf168b862014-06-19 12:32:29 -0700235 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000236};
237
238///////////////////////////////////////////////////////////////////////////////
239
bsalomon00a8fae2014-07-11 08:42:11 -0700240DEF_BENCH( return new AtomicInc32(); )
241DEF_BENCH( return new AtomicInc64(); )
242
mtklein@google.com410e6e82013-09-13 19:52:27 +0000243DEF_BENCH( return new RefCntBench_Stack(); )
244DEF_BENCH( return new RefCntBench_Heap(); )
245DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000246
mtklein@google.com410e6e82013-09-13 19:52:27 +0000247DEF_BENCH( return new WeakRefCntBench_Stack(); )
248DEF_BENCH( return new WeakRefCntBench_Heap(); )
249DEF_BENCH( return new WeakRefCntBench_New(); )