blob: 0110dbcb1317ebd317f82f9b0cfb03ffbbd8e062 [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 */
7#include "SkBenchmark.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +00008#include "SkRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +00009#include "SkThread.h"
bungeman@google.coma02bc152012-05-16 18:21:56 +000010#include "SkWeakRefCnt.h"
bungeman@google.com55487522012-05-14 14:09:24 +000011#include <memory>
12
13enum {
mtklein@google.comc2897432013-09-10 19:23:38 +000014 M = 2
bungeman@google.com55487522012-05-14 14:09:24 +000015};
16
17class RefCntBench_Stack : public SkBenchmark {
18public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000019 RefCntBench_Stack() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000020 fIsRendering = false;
bungeman@google.com55487522012-05-14 14:09:24 +000021 }
22protected:
23 virtual const char* onGetName() {
24 return "ref_cnt_stack";
25 }
26
sugoi@google.com77472f02013-03-05 18:50:01 +000027 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +000028 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000029 SkRefCnt ref;
30 for (int j = 0; j < M; ++j) {
31 ref.ref();
32 ref.unref();
33 }
34 }
35 }
36
37private:
38 typedef SkBenchmark INHERITED;
39};
40
41class PlacedRefCnt : public SkRefCnt {
42public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000043 SK_DECLARE_INST_COUNT(PlacedRefCnt)
44
bungeman@google.com55487522012-05-14 14:09:24 +000045 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000046 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000047
48private:
49 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000050};
51
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000052SK_DEFINE_INST_COUNT(PlacedRefCnt)
53
bungeman@google.com55487522012-05-14 14:09:24 +000054class RefCntBench_Heap : public SkBenchmark {
55public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000056 RefCntBench_Heap() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000057 fIsRendering = false;
bungeman@google.com55487522012-05-14 14:09:24 +000058 }
59protected:
60 virtual const char* onGetName() {
61 return "ref_cnt_heap";
62 }
63
sugoi@google.com77472f02013-03-05 18:50:01 +000064 virtual void onDraw(SkCanvas*) {
bungeman@google.com55487522012-05-14 14:09:24 +000065 char memory[sizeof(PlacedRefCnt)];
mtklein@google.comc2897432013-09-10 19:23:38 +000066 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.com55487522012-05-14 14:09:24 +000067 PlacedRefCnt* ref = new (memory) PlacedRefCnt();
68 for (int j = 0; j < M; ++j) {
69 ref->ref();
70 ref->unref();
71 }
72 ref->unref();
73 }
74 }
75
76private:
77 typedef SkBenchmark INHERITED;
78};
79
bungeman@google.coma02bc152012-05-16 18:21:56 +000080class RefCntBench_New : public SkBenchmark {
81public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000082 RefCntBench_New() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000083 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +000084 }
85protected:
86 virtual const char* onGetName() {
87 return "ref_cnt_new";
88 }
89
sugoi@google.com77472f02013-03-05 18:50:01 +000090 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +000091 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000092 SkRefCnt* ref = new SkRefCnt();
93 for (int j = 0; j < M; ++j) {
94 ref->ref();
95 ref->unref();
96 }
97 ref->unref();
98 }
99 }
100
101private:
102 typedef SkBenchmark INHERITED;
103};
104
bungeman@google.com55487522012-05-14 14:09:24 +0000105///////////////////////////////////////////////////////////////////////////////
106
bungeman@google.coma02bc152012-05-16 18:21:56 +0000107class WeakRefCntBench_Stack : public SkBenchmark {
108public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000109 WeakRefCntBench_Stack() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000110 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000111 }
112protected:
113 virtual const char* onGetName() {
114 return "ref_cnt_stack_weak";
115 }
bungeman@google.com55487522012-05-14 14:09:24 +0000116
sugoi@google.com77472f02013-03-05 18:50:01 +0000117 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000118 for (int i = 0; i < this->getLoops(); ++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:
128 typedef SkBenchmark INHERITED;
129};
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
137class WeakRefCntBench_Heap : public SkBenchmark {
138public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000139 WeakRefCntBench_Heap() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000140 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000141 }
142protected:
143 virtual const char* onGetName() {
144 return "ref_cnt_heap_weak";
145 }
146
sugoi@google.com77472f02013-03-05 18:50:01 +0000147 virtual void onDraw(SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000148 char memory[sizeof(PlacedWeakRefCnt)];
mtklein@google.comc2897432013-09-10 19:23:38 +0000149 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000150 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
151 for (int j = 0; j < M; ++j) {
152 ref->ref();
153 ref->unref();
154 }
155 ref->unref();
156 }
157 }
158
159private:
160 typedef SkBenchmark INHERITED;
161};
162
163class WeakRefCntBench_New : public SkBenchmark {
164public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000165 WeakRefCntBench_New() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000166 fIsRendering = false;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000167 }
168protected:
169 virtual const char* onGetName() {
170 return "ref_cnt_new_weak";
171 }
172
sugoi@google.com77472f02013-03-05 18:50:01 +0000173 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000174 for (int i = 0; i < this->getLoops(); ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000175 SkWeakRefCnt* ref = new SkWeakRefCnt();
176 for (int j = 0; j < M; ++j) {
177 ref->ref();
178 ref->unref();
179 }
180 ref->unref();
181 }
182 }
183
184private:
185 typedef SkBenchmark INHERITED;
186};
187
188///////////////////////////////////////////////////////////////////////////////
189
mtklein@google.com410e6e82013-09-13 19:52:27 +0000190DEF_BENCH( return new RefCntBench_Stack(); )
191DEF_BENCH( return new RefCntBench_Heap(); )
192DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000193
mtklein@google.com410e6e82013-09-13 19:52:27 +0000194DEF_BENCH( return new WeakRefCntBench_Stack(); )
195DEF_BENCH( return new WeakRefCntBench_Heap(); )
196DEF_BENCH( return new WeakRefCntBench_New(); )