blob: 351513b83182edccbfaac3baf26ed0f3b4b9dde0 [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
tfarinaf168b862014-06-19 12:32:29 -070017class RefCntBench_Stack : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000018public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000019 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
20 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:
24 virtual const char* onGetName() {
25 return "ref_cnt_stack";
26 }
27
commit-bot@chromium.org33614712013-12-03 18:17:16 +000028 virtual void onDraw(const int loops, SkCanvas*) {
29 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:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000044 SK_DECLARE_INST_COUNT(PlacedRefCnt)
45
bungeman@google.com55487522012-05-14 14:09:24 +000046 PlacedRefCnt() : SkRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +000047 void operator delete(void*) { }
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000048
49private:
50 typedef SkRefCnt INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000051};
52
tfarinaf168b862014-06-19 12:32:29 -070053class RefCntBench_Heap : public Benchmark {
bungeman@google.com55487522012-05-14 14:09:24 +000054public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000055 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
56 return backend == kNonRendering_Backend;
bungeman@google.com55487522012-05-14 14:09:24 +000057 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000058
bungeman@google.com55487522012-05-14 14:09:24 +000059protected:
60 virtual const char* onGetName() {
61 return "ref_cnt_heap";
62 }
63
commit-bot@chromium.org33614712013-12-03 18:17:16 +000064 virtual void onDraw(const int loops, SkCanvas*) {
bungeman@google.com55487522012-05-14 14:09:24 +000065 char memory[sizeof(PlacedRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +000066 for (int i = 0; i < loops; ++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:
tfarinaf168b862014-06-19 12:32:29 -070077 typedef Benchmark INHERITED;
bungeman@google.com55487522012-05-14 14:09:24 +000078};
79
tfarinaf168b862014-06-19 12:32:29 -070080class RefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +000081public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000082 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
83 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +000084 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000085
bungeman@google.coma02bc152012-05-16 18:21:56 +000086protected:
87 virtual const char* onGetName() {
88 return "ref_cnt_new";
89 }
90
commit-bot@chromium.org33614712013-12-03 18:17:16 +000091 virtual void onDraw(const int loops, SkCanvas*) {
92 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +000093 SkRefCnt* ref = new SkRefCnt();
94 for (int j = 0; j < M; ++j) {
95 ref->ref();
96 ref->unref();
97 }
98 ref->unref();
99 }
100 }
101
102private:
tfarinaf168b862014-06-19 12:32:29 -0700103 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000104};
105
bungeman@google.com55487522012-05-14 14:09:24 +0000106///////////////////////////////////////////////////////////////////////////////
107
tfarinaf168b862014-06-19 12:32:29 -0700108class WeakRefCntBench_Stack : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000109public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000110 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
111 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000112 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000113
bungeman@google.coma02bc152012-05-16 18:21:56 +0000114protected:
115 virtual const char* onGetName() {
116 return "ref_cnt_stack_weak";
117 }
bungeman@google.com55487522012-05-14 14:09:24 +0000118
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000119 virtual void onDraw(const int loops, SkCanvas*) {
120 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000121 SkWeakRefCnt ref;
122 for (int j = 0; j < M; ++j) {
123 ref.ref();
124 ref.unref();
125 }
126 }
127 }
bungeman@google.com55487522012-05-14 14:09:24 +0000128
bungeman@google.coma02bc152012-05-16 18:21:56 +0000129private:
tfarinaf168b862014-06-19 12:32:29 -0700130 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000131};
132
133class PlacedWeakRefCnt : public SkWeakRefCnt {
134public:
135 PlacedWeakRefCnt() : SkWeakRefCnt() { }
sugoi@google.com77472f02013-03-05 18:50:01 +0000136 void operator delete(void*) { }
bungeman@google.coma02bc152012-05-16 18:21:56 +0000137};
138
tfarinaf168b862014-06-19 12:32:29 -0700139class WeakRefCntBench_Heap : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000140public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000141 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
142 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000143 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000144
bungeman@google.coma02bc152012-05-16 18:21:56 +0000145protected:
146 virtual const char* onGetName() {
147 return "ref_cnt_heap_weak";
148 }
149
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000150 virtual void onDraw(const int loops, SkCanvas*) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000151 char memory[sizeof(PlacedWeakRefCnt)];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000152 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000153 PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
154 for (int j = 0; j < M; ++j) {
155 ref->ref();
156 ref->unref();
157 }
158 ref->unref();
159 }
160 }
161
162private:
tfarinaf168b862014-06-19 12:32:29 -0700163 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000164};
165
tfarinaf168b862014-06-19 12:32:29 -0700166class WeakRefCntBench_New : public Benchmark {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000167public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000168 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
169 return backend == kNonRendering_Backend;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000170 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000171
bungeman@google.coma02bc152012-05-16 18:21:56 +0000172protected:
173 virtual const char* onGetName() {
174 return "ref_cnt_new_weak";
175 }
176
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000177 virtual void onDraw(const int loops, SkCanvas*) {
178 for (int i = 0; i < loops; ++i) {
bungeman@google.coma02bc152012-05-16 18:21:56 +0000179 SkWeakRefCnt* ref = new SkWeakRefCnt();
180 for (int j = 0; j < M; ++j) {
181 ref->ref();
182 ref->unref();
183 }
184 ref->unref();
185 }
186 }
187
188private:
tfarinaf168b862014-06-19 12:32:29 -0700189 typedef Benchmark INHERITED;
bungeman@google.coma02bc152012-05-16 18:21:56 +0000190};
191
192///////////////////////////////////////////////////////////////////////////////
193
mtklein@google.com410e6e82013-09-13 19:52:27 +0000194DEF_BENCH( return new RefCntBench_Stack(); )
195DEF_BENCH( return new RefCntBench_Heap(); )
196DEF_BENCH( return new RefCntBench_New(); )
bungeman@google.coma02bc152012-05-16 18:21:56 +0000197
mtklein@google.com410e6e82013-09-13 19:52:27 +0000198DEF_BENCH( return new WeakRefCntBench_Stack(); )
199DEF_BENCH( return new WeakRefCntBench_Heap(); )
200DEF_BENCH( return new WeakRefCntBench_New(); )