Avoid loop-dependent behavior in GrMemoryPoolBench
This helps stability of benchmark across repeated runs, and across code changes. Previously, a change to the tuned loop count could radically change the allocation behavior within the loop's iteration and lead to unfair comparisons. In addition, this separates the stack allocation pattern into N allocations followed by N LIFO releases, and a push-pop alternating pattern of N allocates and releases (so still LIFO, but reuses the memory at the start of a block). In later CLs experimenting on the memory pool, I found that there were surprising effects on performance linked to the specific interaction between the allocation size, per-allocation metadata, and per-block metadata. To help differentiate these coincidences, this adds two modes of allocation where one should already be aligned. It also moves away from a global pool, so that it's possible to benchmark on different block sizes and factor in the allocation/release cost of the actual blocks (vs. the cursor management of a larger sized pool). As part of this, the new/delete reference operator is added as an explicit benchmark. Change-Id: I12b8c11cb75db0df70460fe2e8cf6c029db7eb22 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/262936 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Brian Salomon <bsalomon@google.com>
This commit is contained in:
parent
fa424b2e6a
commit
f811fc331a
@ -5,185 +5,204 @@
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "include/core/SkTypes.h"
|
||||
|
||||
#include "bench/Benchmark.h"
|
||||
#include "include/private/SkTDArray.h"
|
||||
#include "include/private/SkTemplates.h"
|
||||
#include "include/private/GrTypesPriv.h"
|
||||
#include "include/utils/SkRandom.h"
|
||||
#include "src/gpu/GrMemoryPool.h"
|
||||
|
||||
#include <new>
|
||||
|
||||
// change this to 0 to compare GrMemoryPool to default new / delete
|
||||
#define OVERRIDE_NEW 1
|
||||
|
||||
struct A {
|
||||
int gStuff[10];
|
||||
#if OVERRIDE_NEW
|
||||
void* operator new(size_t size) { return gBenchPool->allocate(size); }
|
||||
void operator delete(void* mem) {
|
||||
if (mem) {
|
||||
return gBenchPool->release(mem);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
static std::unique_ptr<GrMemoryPool> gBenchPool;
|
||||
// sizeof is a multiple of GrMemoryPool::kAlignment
|
||||
struct Aligned {
|
||||
int fStuff[12]; // Will align on 4, 8, or 16 alignment
|
||||
};
|
||||
std::unique_ptr<GrMemoryPool> A::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
|
||||
static_assert(sizeof(Aligned) % GrMemoryPool::kAlignment == 0);
|
||||
|
||||
/**
|
||||
* This benchmark creates and deletes objects in stack order
|
||||
*/
|
||||
class GrMemoryPoolBenchStack : public Benchmark {
|
||||
public:
|
||||
bool isSuitableFor(Backend backend) override {
|
||||
return backend == kNonRendering_Backend;
|
||||
}
|
||||
// sizeof is not a multiple of GrMemoryPool::kAlignment
|
||||
struct Unaligned {
|
||||
int fStuff[9]; // Will not align on 8 or 16, but will on 4...
|
||||
};
|
||||
static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0);
|
||||
|
||||
protected:
|
||||
const char* onGetName() override {
|
||||
return "grmemorypool_stack";
|
||||
}
|
||||
// All benchmarks create and delete the same number of objects. The key difference is the order
|
||||
// of operations, the size of the objects being allocated, and the size of the pool.
|
||||
typedef void (*RunBenchProc)(GrMemoryPool*, int);
|
||||
|
||||
void onDraw(int loops, SkCanvas*) override {
|
||||
SkRandom r;
|
||||
enum {
|
||||
kMaxObjects = 4 * (1 << 10),
|
||||
};
|
||||
A* objects[kMaxObjects];
|
||||
|
||||
// We delete if a random number [-1, 1] is < the thresh. Otherwise,
|
||||
// we allocate. We start allocate-biased and ping-pong to delete-biased
|
||||
SkScalar delThresh = -SK_ScalarHalf;
|
||||
const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
|
||||
int s = 0;
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < loops; i++, ++s) {
|
||||
if (kSwitchThreshPeriod == s) {
|
||||
delThresh = -delThresh;
|
||||
s = 0;
|
||||
}
|
||||
SkScalar del = r.nextSScalar1();
|
||||
if (count &&
|
||||
(kMaxObjects == count || del < delThresh)) {
|
||||
delete objects[count-1];
|
||||
--count;
|
||||
// N objects are created, and then destroyed in reverse order (fully unwinding the cursor within
|
||||
// each block of the memory pool).
|
||||
template <typename T>
|
||||
static void run_stack(GrMemoryPool* pool, int loops) {
|
||||
static const int kMaxObjects = 4 * (1 << 10);
|
||||
T* objs[kMaxObjects];
|
||||
for (int i = 0; i < loops; ++i) {
|
||||
// Push N objects into the pool (or heap if pool is null)
|
||||
for (int j = 0; j < kMaxObjects; ++j) {
|
||||
objs[j] = pool ? (T*) pool->allocate(sizeof(T)) : new T;
|
||||
}
|
||||
// Pop N objects off in LIFO order
|
||||
for (int j = kMaxObjects - 1; j >= 0; --j) {
|
||||
if (pool) {
|
||||
pool->release(objs[j]);
|
||||
} else {
|
||||
objects[count] = new A;
|
||||
++count;
|
||||
delete objs[j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < count; ++i) {
|
||||
delete objects[i];
|
||||
|
||||
// Everything has been cleaned up for the next loop
|
||||
}
|
||||
}
|
||||
|
||||
// N objects are created, and then destroyed in creation order (is not able to unwind the cursor
|
||||
// within each block, but can reclaim the block once everything is destroyed).
|
||||
template <typename T>
|
||||
static void run_queue(GrMemoryPool* pool, int loops) {
|
||||
static const int kMaxObjects = 4 * (1 << 10);
|
||||
T* objs[kMaxObjects];
|
||||
for (int i = 0; i < loops; ++i) {
|
||||
// Push N objects into the pool (or heap if pool is null)
|
||||
for (int j = 0; j < kMaxObjects; ++j) {
|
||||
objs[j] = pool ? (T*) pool->allocate(sizeof(T)) : new T;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
typedef Benchmark INHERITED;
|
||||
};
|
||||
|
||||
struct B {
|
||||
int gStuff[10];
|
||||
#if OVERRIDE_NEW
|
||||
void* operator new(size_t size) { return gBenchPool->allocate(size); }
|
||||
void operator delete(void* mem) {
|
||||
if (mem) {
|
||||
return gBenchPool->release(mem);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
static std::unique_ptr<GrMemoryPool> gBenchPool;
|
||||
};
|
||||
std::unique_ptr<GrMemoryPool> B::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
|
||||
|
||||
/**
|
||||
* This benchmark creates objects and deletes them in random order
|
||||
*/
|
||||
class GrMemoryPoolBenchRandom : public Benchmark {
|
||||
public:
|
||||
bool isSuitableFor(Backend backend) override {
|
||||
return backend == kNonRendering_Backend;
|
||||
}
|
||||
|
||||
protected:
|
||||
const char* onGetName() override {
|
||||
return "grmemorypool_random";
|
||||
}
|
||||
|
||||
void onDraw(int loops, SkCanvas*) override {
|
||||
SkRandom r;
|
||||
enum {
|
||||
kMaxObjects = 4 * (1 << 10),
|
||||
};
|
||||
std::unique_ptr<B> objects[kMaxObjects];
|
||||
|
||||
for (int i = 0; i < loops; i++) {
|
||||
uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
|
||||
if (nullptr == objects[idx].get()) {
|
||||
objects[idx].reset(new B);
|
||||
// Pop N objects off in FIFO order
|
||||
for (int j = 0; j < kMaxObjects; ++j) {
|
||||
if (pool) {
|
||||
pool->release(objs[j]);
|
||||
} else {
|
||||
objects[idx].reset();
|
||||
delete objs[j];
|
||||
}
|
||||
}
|
||||
|
||||
// Everything has been cleaned up for the next loop
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
typedef Benchmark INHERITED;
|
||||
};
|
||||
|
||||
struct C {
|
||||
int gStuff[10];
|
||||
#if OVERRIDE_NEW
|
||||
void* operator new(size_t size) { return gBenchPool->allocate(size); }
|
||||
void operator delete(void* mem) {
|
||||
if (mem) {
|
||||
return gBenchPool->release(mem);
|
||||
// N objects are created and immediately destroyed, so space at the start of the pool should be
|
||||
// immediately reclaimed.
|
||||
template <typename T>
|
||||
static void run_pushpop(GrMemoryPool* pool, int loops) {
|
||||
static const int kMaxObjects = 4 * (1 << 10);
|
||||
T* objs[kMaxObjects];
|
||||
for (int i = 0; i < loops; ++i) {
|
||||
// Push N objects into the pool (or heap if pool is null)
|
||||
for (int j = 0; j < kMaxObjects; ++j) {
|
||||
if (pool) {
|
||||
objs[j] = (T*) pool->allocate(sizeof(T));
|
||||
pool->release(objs[j]);
|
||||
} else {
|
||||
objs[j] = new T;
|
||||
delete objs[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
static std::unique_ptr<GrMemoryPool> gBenchPool;
|
||||
};
|
||||
std::unique_ptr<GrMemoryPool> C::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
|
||||
|
||||
/**
|
||||
* This benchmark creates objects and deletes them in queue order
|
||||
*/
|
||||
class GrMemoryPoolBenchQueue : public Benchmark {
|
||||
enum {
|
||||
M = 4 * (1 << 10),
|
||||
// Everything has been cleaned up for the next loop
|
||||
}
|
||||
}
|
||||
|
||||
// N object creations and destructions are invoked in random order.
|
||||
template <typename T>
|
||||
static void run_random(GrMemoryPool* pool, int loops) {
|
||||
static const int kMaxObjects = 4 * (1 << 10);
|
||||
T* objs[kMaxObjects];
|
||||
for (int i = 0; i < kMaxObjects; ++i) {
|
||||
objs[i] = nullptr;
|
||||
}
|
||||
|
||||
auto del = [&](int j) {
|
||||
// Delete
|
||||
if (pool) {
|
||||
pool->release(objs[j]);
|
||||
} else {
|
||||
delete objs[j];
|
||||
}
|
||||
objs[j] = nullptr;
|
||||
};
|
||||
|
||||
SkRandom r;
|
||||
for (int i = 0; i < loops; ++i) {
|
||||
// Execute 2*kMaxObjects operations, which should average to N create and N destroy,
|
||||
// followed by a small number of remaining deletions.
|
||||
for (int j = 0; j < 2 * kMaxObjects; ++j) {
|
||||
int k = r.nextRangeU(0, kMaxObjects-1);
|
||||
if (objs[k]) {
|
||||
del(k);
|
||||
} else {
|
||||
// Create
|
||||
objs[k] = pool ? (T*) pool->allocate(sizeof(T)) : new T;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure everything is null for the next loop
|
||||
for (int j = 0; j < kMaxObjects; ++j) {
|
||||
if (objs[j]) {
|
||||
del(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class GrMemoryPoolBench : public Benchmark {
|
||||
public:
|
||||
GrMemoryPoolBench(const char* name, RunBenchProc proc, int poolSize)
|
||||
: fPoolSize(poolSize)
|
||||
, fProc(proc) {
|
||||
fName.printf("grmemorypool_%s", name);
|
||||
}
|
||||
|
||||
bool isSuitableFor(Backend backend) override {
|
||||
return backend == kNonRendering_Backend;
|
||||
}
|
||||
|
||||
protected:
|
||||
const char* onGetName() override {
|
||||
return "grmemorypool_queue";
|
||||
return fName.c_str();
|
||||
}
|
||||
|
||||
void onDraw(int loops, SkCanvas*) override {
|
||||
SkRandom r;
|
||||
C* objects[M];
|
||||
for (int i = 0; i < loops; i++) {
|
||||
uint32_t count = r.nextRangeU(0, M-1);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
objects[i] = new C;
|
||||
}
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
delete objects[i];
|
||||
}
|
||||
}
|
||||
std::unique_ptr<GrMemoryPool> pool;
|
||||
if (fPoolSize > 0) {
|
||||
pool = GrMemoryPool::Make(fPoolSize, fPoolSize);
|
||||
} // else keep it null to test regular new/delete performance
|
||||
|
||||
fProc(pool.get(), loops);
|
||||
}
|
||||
|
||||
private:
|
||||
SkString fName;
|
||||
int fPoolSize;
|
||||
RunBenchProc fProc;
|
||||
|
||||
typedef Benchmark INHERITED;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DEF_BENCH( return new GrMemoryPoolBenchStack(); )
|
||||
DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
|
||||
DEF_BENCH( return new GrMemoryPoolBenchQueue(); )
|
||||
static const int kLargePool = 10 * (1 << 10);
|
||||
static const int kSmallPool = GrMemoryPool::kMinAllocationSize;
|
||||
|
||||
DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_lg", run_stack<Aligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_sm", run_stack<Aligned>, kSmallPool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_ref", run_stack<Aligned>, 0); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_lg", run_stack<Unaligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_sm", run_stack<Unaligned>, kSmallPool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_ref", run_stack<Unaligned>, 0); )
|
||||
|
||||
DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_lg", run_queue<Aligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_sm", run_queue<Aligned>, kSmallPool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_ref", run_queue<Aligned>, 0); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_lg", run_queue<Unaligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_sm", run_queue<Unaligned>, kSmallPool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_ref", run_queue<Unaligned>, 0); )
|
||||
|
||||
DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_lg", run_pushpop<Aligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_sm", run_pushpop<Aligned>, kSmallPool); )
|
||||
// DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_ref", run_pushpop<Aligned>, 0); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_lg", run_pushpop<Unaligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_sm", run_pushpop<Unaligned>, kSmallPool); )
|
||||
// DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_ref", run_pushpop<Unaligned>, 0); )
|
||||
// pushpop_x_ref are not meaningful because the compiler completely optimizes away new T; delete *.
|
||||
|
||||
DEF_BENCH( return new GrMemoryPoolBench("random_aligned_lg", run_random<Aligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("random_aligned_sm", run_random<Aligned>, kSmallPool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("random_aligned_ref", run_random<Aligned>, 0); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_lg", run_random<Unaligned>, kLargePool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_sm", run_random<Unaligned>, kSmallPool); )
|
||||
DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_ref", run_random<Unaligned>, 0); )
|
||||
|
Loading…
Reference in New Issue
Block a user