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 "SkBenchmark.h" 8 #include "SkRefCnt.h" 9 #include "SkThread.h" 10 #include "SkWeakRefCnt.h" 11 #include <memory> 12 13 enum { 14 N = SkBENCHLOOP(100000), 15 M = SkBENCHLOOP(2) 16 }; 17 18 class RefCntBench_Stack : public SkBenchmark { 19 public: 20 RefCntBench_Stack(void* param) : INHERITED(param) { 21 fIsRendering = false; 22 } 23 protected: 24 virtual const char* onGetName() { 25 return "ref_cnt_stack"; 26 } 27 28 virtual void onDraw(SkCanvas* canvas) { 29 for (int i = 0; i < N; ++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 SkBenchmark INHERITED; 40 }; 41 42 class PlacedRefCnt : public SkRefCnt { 43 public: 44 SK_DECLARE_INST_COUNT(PlacedRefCnt) 45 46 PlacedRefCnt() : SkRefCnt() { } 47 void operator delete(void *p) { } 48 49 private: 50 typedef SkRefCnt INHERITED; 51 }; 52 53 SK_DEFINE_INST_COUNT(PlacedRefCnt) 54 55 class RefCntBench_Heap : public SkBenchmark { 56 public: 57 RefCntBench_Heap(void* param) : INHERITED(param) { 58 fIsRendering = false; 59 } 60 protected: 61 virtual const char* onGetName() { 62 return "ref_cnt_heap"; 63 } 64 65 virtual void onDraw(SkCanvas* canvas) { 66 char memory[sizeof(PlacedRefCnt)]; 67 for (int i = 0; i < N; ++i) { 68 PlacedRefCnt* ref = new (memory) PlacedRefCnt(); 69 for (int j = 0; j < M; ++j) { 70 ref->ref(); 71 ref->unref(); 72 } 73 ref->unref(); 74 } 75 } 76 77 private: 78 typedef SkBenchmark INHERITED; 79 }; 80 81 class RefCntBench_New : public SkBenchmark { 82 public: 83 RefCntBench_New(void* param) : INHERITED(param) { 84 fIsRendering = false; 85 } 86 protected: 87 virtual const char* onGetName() { 88 return "ref_cnt_new"; 89 } 90 91 virtual void onDraw(SkCanvas* canvas) { 92 for (int i = 0; i < N; ++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 SkBenchmark INHERITED; 104 }; 105 106 /////////////////////////////////////////////////////////////////////////////// 107 108 class WeakRefCntBench_Stack : public SkBenchmark { 109 public: 110 WeakRefCntBench_Stack(void* param) : INHERITED(param) { 111 fIsRendering = false; 112 } 113 protected: 114 virtual const char* onGetName() { 115 return "ref_cnt_stack_weak"; 116 } 117 118 virtual void onDraw(SkCanvas* canvas) { 119 for (int i = 0; i < N; ++i) { 120 SkWeakRefCnt ref; 121 for (int j = 0; j < M; ++j) { 122 ref.ref(); 123 ref.unref(); 124 } 125 } 126 } 127 128 private: 129 typedef SkBenchmark INHERITED; 130 }; 131 132 class PlacedWeakRefCnt : public SkWeakRefCnt { 133 public: 134 PlacedWeakRefCnt() : SkWeakRefCnt() { } 135 void operator delete(void *p) { } 136 }; 137 138 class WeakRefCntBench_Heap : public SkBenchmark { 139 public: 140 WeakRefCntBench_Heap(void* param) : INHERITED(param) { 141 fIsRendering = false; 142 } 143 protected: 144 virtual const char* onGetName() { 145 return "ref_cnt_heap_weak"; 146 } 147 148 virtual void onDraw(SkCanvas* canvas) { 149 char memory[sizeof(PlacedWeakRefCnt)]; 150 for (int i = 0; i < N; ++i) { 151 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 160 private: 161 typedef SkBenchmark INHERITED; 162 }; 163 164 class WeakRefCntBench_New : public SkBenchmark { 165 public: 166 WeakRefCntBench_New(void* param) : INHERITED(param) { 167 fIsRendering = false; 168 } 169 protected: 170 virtual const char* onGetName() { 171 return "ref_cnt_new_weak"; 172 } 173 174 virtual void onDraw(SkCanvas* canvas) { 175 for (int i = 0; i < N; ++i) { 176 SkWeakRefCnt* ref = new SkWeakRefCnt(); 177 for (int j = 0; j < M; ++j) { 178 ref->ref(); 179 ref->unref(); 180 } 181 ref->unref(); 182 } 183 } 184 185 private: 186 typedef SkBenchmark INHERITED; 187 }; 188 189 /////////////////////////////////////////////////////////////////////////////// 190 191 static SkBenchmark* Fact00(void* p) { return new RefCntBench_Stack(p); } 192 static SkBenchmark* Fact01(void* p) { return new RefCntBench_Heap(p); } 193 static SkBenchmark* Fact02(void* p) { return new RefCntBench_New(p); } 194 195 static SkBenchmark* Fact10(void* p) { return new WeakRefCntBench_Stack(p); } 196 static SkBenchmark* Fact11(void* p) { return new WeakRefCntBench_Heap(p); } 197 static SkBenchmark* Fact12(void* p) { return new WeakRefCntBench_New(p); } 198 199 static BenchRegistry gReg00(Fact00); 200 static BenchRegistry gReg01(Fact01); 201 static BenchRegistry gReg02(Fact02); 202 203 static BenchRegistry gReg10(Fact10); 204 static BenchRegistry gReg11(Fact11); 205 static BenchRegistry gReg12(Fact12); 206