• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <memory>
8 #include "Benchmark.h"
9 #include "SkRefCnt.h"
10 #include "SkThread.h"
11 #include "SkWeakRefCnt.h"
12 
13 enum {
14     M = 2
15 };
16 
17 class RefCntBench_Stack : public Benchmark {
18 public:
isSuitableFor(Backend backend)19     virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
20         return backend == kNonRendering_Backend;
21     }
22 
23 protected:
onGetName()24     virtual const char* onGetName() {
25         return "ref_cnt_stack";
26     }
27 
onDraw(const int loops,SkCanvas *)28     virtual void onDraw(const int loops, SkCanvas*) {
29         for (int i = 0; i < loops; ++i) {
30             SkRefCnt ref;
31             for (int j = 0; j < M; ++j) {
32                 ref.ref();
33                 ref.unref();
34             }
35         }
36     }
37 
38 private:
39     typedef Benchmark INHERITED;
40 };
41 
42 class PlacedRefCnt : public SkRefCnt {
43 public:
44     SK_DECLARE_INST_COUNT(PlacedRefCnt)
45 
PlacedRefCnt()46     PlacedRefCnt() : SkRefCnt() { }
operator delete(void *)47     void operator delete(void*) { }
48 
49 private:
50     typedef SkRefCnt INHERITED;
51 };
52 
53 class RefCntBench_Heap : public Benchmark {
54 public:
isSuitableFor(Backend backend)55     virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
56         return backend == kNonRendering_Backend;
57     }
58 
59 protected:
onGetName()60     virtual const char* onGetName() {
61         return "ref_cnt_heap";
62     }
63 
onDraw(const int loops,SkCanvas *)64     virtual void onDraw(const int loops, SkCanvas*) {
65         char memory[sizeof(PlacedRefCnt)];
66         for (int i = 0; i < loops; ++i) {
67             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 
76 private:
77     typedef Benchmark INHERITED;
78 };
79 
80 class RefCntBench_New : public Benchmark {
81 public:
isSuitableFor(Backend backend)82     virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
83         return backend == kNonRendering_Backend;
84     }
85 
86 protected:
onGetName()87     virtual const char* onGetName() {
88         return "ref_cnt_new";
89     }
90 
onDraw(const int loops,SkCanvas *)91     virtual void onDraw(const int loops, SkCanvas*) {
92         for (int i = 0; i < loops; ++i) {
93             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 
102 private:
103     typedef Benchmark INHERITED;
104 };
105 
106 ///////////////////////////////////////////////////////////////////////////////
107 
108 class WeakRefCntBench_Stack : public Benchmark {
109 public:
isSuitableFor(Backend backend)110     virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
111         return backend == kNonRendering_Backend;
112     }
113 
114 protected:
onGetName()115     virtual const char* onGetName() {
116         return "ref_cnt_stack_weak";
117     }
118 
onDraw(const int loops,SkCanvas *)119     virtual void onDraw(const int loops, SkCanvas*) {
120         for (int i = 0; i < loops; ++i) {
121             SkWeakRefCnt ref;
122             for (int j = 0; j < M; ++j) {
123                 ref.ref();
124                 ref.unref();
125             }
126         }
127     }
128 
129 private:
130     typedef Benchmark INHERITED;
131 };
132 
133 class PlacedWeakRefCnt : public SkWeakRefCnt {
134 public:
PlacedWeakRefCnt()135     PlacedWeakRefCnt() : SkWeakRefCnt() { }
operator delete(void *)136     void operator delete(void*) { }
137 };
138 
139 class WeakRefCntBench_Heap : public Benchmark {
140 public:
isSuitableFor(Backend backend)141     virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
142         return backend == kNonRendering_Backend;
143     }
144 
145 protected:
onGetName()146     virtual const char* onGetName() {
147         return "ref_cnt_heap_weak";
148     }
149 
onDraw(const int loops,SkCanvas *)150     virtual void onDraw(const int loops, SkCanvas*) {
151         char memory[sizeof(PlacedWeakRefCnt)];
152         for (int i = 0; i < loops; ++i) {
153             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 
162 private:
163     typedef Benchmark INHERITED;
164 };
165 
166 class WeakRefCntBench_New : public Benchmark {
167 public:
isSuitableFor(Backend backend)168     virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
169         return backend == kNonRendering_Backend;
170     }
171 
172 protected:
onGetName()173     virtual const char* onGetName() {
174         return "ref_cnt_new_weak";
175     }
176 
onDraw(const int loops,SkCanvas *)177     virtual void onDraw(const int loops, SkCanvas*) {
178         for (int i = 0; i < loops; ++i) {
179             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 
188 private:
189     typedef Benchmark INHERITED;
190 };
191 
192 ///////////////////////////////////////////////////////////////////////////////
193 
194 DEF_BENCH( return new RefCntBench_Stack(); )
195 DEF_BENCH( return new RefCntBench_Heap(); )
196 DEF_BENCH( return new RefCntBench_New(); )
197 
198 DEF_BENCH( return new WeakRefCntBench_Stack(); )
199 DEF_BENCH( return new WeakRefCntBench_Heap(); )
200 DEF_BENCH( return new WeakRefCntBench_New(); )
201