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