blob: af4822a6a781de5f8a456dedcafa0480f860f3f9 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "bench/Benchmark.h"
8#include "include/core/SkRefCnt.h"
9#include "include/private/SkWeakRefCnt.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040010#include <memory>
11#include <new>
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
tfarinaf168b862014-06-19 12:32:29 -070017class RefCntBench_Stack : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000018public:
mtklein36352bf2015-03-25 18:17:31 -070019 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000020 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000021 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000022
bungeman@google.com55487522012-05-14 14:09:24 +000023protected:
mtkleinf0599002015-07-13 06:18:39 -070024 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +000025 return "ref_cnt_stack";
26 }
27
mtkleina1ebeb22015-10-01 09:43:39 -070028 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000029 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000030 SkRefCnt ref;
31 for (int j = 0; j < M; ++j) {
32 ref.ref();
33 ref.unref();
34 }
35 }
36 }
37
38private:
tfarinaf168b862014-06-19 12:32:29 -070039 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000040};
41
42class PlacedRefCnt : public SkRefCnt {
43public:
44 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000045 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000046
47private:
48 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000049};
50
tfarinaf168b862014-06-19 12:32:29 -070051class RefCntBench_Heap : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000052public:
mtklein36352bf2015-03-25 18:17:31 -070053 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000054 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000055 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000056
bungeman@google.com55487522012-05-14 14:09:24 +000057protected:
mtkleinf0599002015-07-13 06:18:39 -070058 const char* onGetName() override {
bungeman@google.com55487522012-05-14 14:09:24 +000059 return "ref_cnt_heap";
60 }
61
mtkleina1ebeb22015-10-01 09:43:39 -070062 void onDraw(int loops, SkCanvas*) override {
bungeman@google.com55487522012-05-14 14:09:24 +000063 char memory[sizeof(PlacedRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +000064 for (int i = 0; i < loops; ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000065 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
74private:
tfarinaf168b862014-06-19 12:32:29 -070075 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000076};
77
tfarinaf168b862014-06-19 12:32:29 -070078class RefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +000079public:
mtklein36352bf2015-03-25 18:17:31 -070080 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000081 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +000082 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000083
bungeman@google.coma02bc152012-05-16 18:21:56 +000084protected:
mtkleinf0599002015-07-13 06:18:39 -070085 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +000086 return "ref_cnt_new";
87 }
88
mtkleina1ebeb22015-10-01 09:43:39 -070089 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000090 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000091 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
100private:
tfarinaf168b862014-06-19 12:32:29 -0700101 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000102};
103
bungeman@google.com55487522012-05-14 14:09:24 +0000104///////////////////////////////////////////////////////////////////////////////
105
tfarinaf168b862014-06-19 12:32:29 -0700106class WeakRefCntBench_Stack : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000107public:
mtklein36352bf2015-03-25 18:17:31 -0700108 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000109 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000110 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000111
bungeman@google.coma02bc152012-05-16 18:21:56 +0000112protected:
mtkleinf0599002015-07-13 06:18:39 -0700113 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000114 return "ref_cnt_stack_weak";
115 }
bungeman@google.com55487522012-05-14 14:09:24 +0000116
mtkleina1ebeb22015-10-01 09:43:39 -0700117 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000118 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000119 SkWeakRefCnt ref;
120 for (int j = 0; j < M; ++j) {
121 ref.ref();
122 ref.unref();
123 }
124 }
125 }
bungeman@google.com55487522012-05-14 14:09:24 +0000126
bungeman@google.coma02bc152012-05-16 18:21:56 +0000127private:
tfarinaf168b862014-06-19 12:32:29 -0700128 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000129};
130
131class PlacedWeakRefCnt : public SkWeakRefCnt {
132public:
133 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000134 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000135};
136
tfarinaf168b862014-06-19 12:32:29 -0700137class WeakRefCntBench_Heap : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000138public:
mtklein36352bf2015-03-25 18:17:31 -0700139 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000140 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000141 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000142
bungeman@google.coma02bc152012-05-16 18:21:56 +0000143protected:
mtklein36352bf2015-03-25 18:17:31 -0700144 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000145 return "ref_cnt_heap_weak";
146 }
147
mtkleina1ebeb22015-10-01 09:43:39 -0700148 void onDraw(int loops, SkCanvas*) override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000149 char memory[sizeof(PlacedWeakRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000150 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000151 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
160private:
tfarinaf168b862014-06-19 12:32:29 -0700161 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000162};
163
tfarinaf168b862014-06-19 12:32:29 -0700164class WeakRefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000165public:
mtklein36352bf2015-03-25 18:17:31 -0700166 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000167 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000168 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000169
bungeman@google.coma02bc152012-05-16 18:21:56 +0000170protected:
mtklein36352bf2015-03-25 18:17:31 -0700171 const char* onGetName() override {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000172 return "ref_cnt_new_weak";
173 }
174
mtkleina1ebeb22015-10-01 09:43:39 -0700175 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000176 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000177 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
186private:
tfarinaf168b862014-06-19 12:32:29 -0700187 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000188};
189
190///////////////////////////////////////////////////////////////////////////////
191
mtklein@google.com410e6e82013-09-13 19:52:27 +0000192DEF_BENCH( return new RefCntBench_Stack(); )
193DEF_BENCH( return new RefCntBench_Heap(); )
194DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000195
mtklein@google.com410e6e82013-09-13 19:52:27 +0000196DEF_BENCH( return new WeakRefCntBench_Stack(); )
197DEF_BENCH( return new WeakRefCntBench_Heap(); )
198DEF_BENCH( return new WeakRefCntBench_New(); )