bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "bench/Benchmark.h" |
| 11 | #include "include/private/SkTDArray.h" |
| 12 | #include "include/private/SkTemplates.h" |
| 13 | #include "include/utils/SkRandom.h" |
| 14 | #include "src/gpu/GrMemoryPool.h" |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 15 | |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 16 | #include <new> |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 17 | |
| 18 | // change this to 0 to compare GrMemoryPool to default new / delete |
| 19 | #define OVERRIDE_NEW 1 |
| 20 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 21 | struct A { |
| 22 | int gStuff[10]; |
| 23 | #if OVERRIDE_NEW |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 24 | void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 25 | void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } } |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 26 | #endif |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 27 | static GrMemoryPool gBenchPool; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 28 | }; |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 29 | GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * This benchmark creates and deletes objects in stack order |
| 33 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 34 | class GrMemoryPoolBenchStack : public Benchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 35 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 37 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 38 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 40 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 41 | const char* onGetName() override { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 42 | return "grmemorypool_stack"; |
| 43 | } |
| 44 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 45 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 46 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 47 | enum { |
| 48 | kMaxObjects = 4 * (1 << 10), |
| 49 | }; |
| 50 | A* objects[kMaxObjects]; |
| 51 | |
benjaminwagner | 1263448 | 2016-03-31 06:13:22 -0700 | [diff] [blame] | 52 | // We delete if a random number [-1, 1] is < the thresh. Otherwise, |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 53 | // we allocate. We start allocate-biased and ping-pong to delete-biased |
benjaminwagner | 1263448 | 2016-03-31 06:13:22 -0700 | [diff] [blame] | 54 | SkScalar delThresh = -SK_ScalarHalf; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 55 | const int kSwitchThreshPeriod = loops / (2 * kMaxObjects); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 56 | int s = 0; |
| 57 | |
| 58 | int count = 0; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 59 | for (int i = 0; i < loops; i++, ++s) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 60 | if (kSwitchThreshPeriod == s) { |
| 61 | delThresh = -delThresh; |
| 62 | s = 0; |
| 63 | } |
benjaminwagner | 1263448 | 2016-03-31 06:13:22 -0700 | [diff] [blame] | 64 | SkScalar del = r.nextSScalar1(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 65 | if (count && |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 66 | (kMaxObjects == count || del < delThresh)) { |
| 67 | delete objects[count-1]; |
| 68 | --count; |
| 69 | } else { |
| 70 | objects[count] = new A; |
| 71 | ++count; |
| 72 | } |
| 73 | } |
| 74 | for (int i = 0; i < count; ++i) { |
| 75 | delete objects[i]; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 80 | typedef Benchmark INHERITED; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 83 | struct B { |
| 84 | int gStuff[10]; |
| 85 | #if OVERRIDE_NEW |
| 86 | void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 87 | void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } } |
| 88 | #endif |
| 89 | static GrMemoryPool gBenchPool; |
| 90 | }; |
| 91 | GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
| 92 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 93 | /** |
| 94 | * This benchmark creates objects and deletes them in random order |
| 95 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 96 | class GrMemoryPoolBenchRandom : public Benchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 97 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 98 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 99 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 100 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 102 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 103 | const char* onGetName() override { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 104 | return "grmemorypool_random"; |
| 105 | } |
| 106 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 107 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 108 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 109 | enum { |
| 110 | kMaxObjects = 4 * (1 << 10), |
| 111 | }; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 112 | std::unique_ptr<B> objects[kMaxObjects]; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 114 | for (int i = 0; i < loops; i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 115 | uint32_t idx = r.nextRangeU(0, kMaxObjects-1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 116 | if (nullptr == objects[idx].get()) { |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 117 | objects[idx].reset(new B); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 118 | } else { |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 119 | objects[idx].reset(); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 125 | typedef Benchmark INHERITED; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 128 | struct C { |
| 129 | int gStuff[10]; |
| 130 | #if OVERRIDE_NEW |
| 131 | void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 132 | void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } } |
| 133 | #endif |
| 134 | static GrMemoryPool gBenchPool; |
| 135 | }; |
| 136 | GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
| 137 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 138 | /** |
| 139 | * This benchmark creates objects and deletes them in queue order |
| 140 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 141 | class GrMemoryPoolBenchQueue : public Benchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 142 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 143 | M = 4 * (1 << 10), |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 144 | }; |
| 145 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 146 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 147 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 148 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 149 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 150 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 151 | const char* onGetName() override { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 152 | return "grmemorypool_queue"; |
| 153 | } |
| 154 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 155 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 156 | SkRandom r; |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 157 | C* objects[M]; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 158 | for (int i = 0; i < loops; i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 159 | uint32_t count = r.nextRangeU(0, M-1); |
| 160 | for (uint32_t i = 0; i < count; i++) { |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 161 | objects[i] = new C; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 162 | } |
| 163 | for (uint32_t i = 0; i < count; i++) { |
| 164 | delete objects[i]; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 170 | typedef Benchmark INHERITED; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /////////////////////////////////////////////////////////////////////////////// |
| 174 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 175 | DEF_BENCH( return new GrMemoryPoolBenchStack(); ) |
| 176 | DEF_BENCH( return new GrMemoryPoolBenchRandom(); ) |
| 177 | DEF_BENCH( return new GrMemoryPoolBenchQueue(); ) |