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