2012-06-19 15:40:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "Benchmark.h"
|
2012-06-19 15:40:27 +00:00
|
|
|
#include "GrMemoryPool.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkTDArray.h"
|
2013-04-18 18:43:26 +00:00
|
|
|
#include "SkTemplates.h"
|
2018-09-19 15:31:27 +00:00
|
|
|
|
2018-06-11 14:45:26 +00:00
|
|
|
#include <new>
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
// change this to 0 to compare GrMemoryPool to default new / delete
|
|
|
|
#define OVERRIDE_NEW 1
|
|
|
|
|
|
|
|
struct A {
|
|
|
|
int gStuff[10];
|
|
|
|
#if OVERRIDE_NEW
|
2014-02-26 23:01:57 +00:00
|
|
|
void* operator new (size_t size) { return gBenchPool.allocate(size); }
|
|
|
|
void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
|
2012-06-19 15:40:27 +00:00
|
|
|
#endif
|
2014-02-26 23:01:57 +00:00
|
|
|
static GrMemoryPool gBenchPool;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
2014-02-26 23:01:57 +00:00
|
|
|
GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This benchmark creates and deletes objects in stack order
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrMemoryPoolBenchStack : public Benchmark {
|
2012-06-19 15:40:27 +00:00
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
protected:
|
2015-07-13 13:18:39 +00:00
|
|
|
const char* onGetName() override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return "grmemorypool_stack";
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom r;
|
2012-06-19 15:40:27 +00:00
|
|
|
enum {
|
|
|
|
kMaxObjects = 4 * (1 << 10),
|
|
|
|
};
|
|
|
|
A* objects[kMaxObjects];
|
|
|
|
|
2016-03-31 13:13:22 +00:00
|
|
|
// We delete if a random number [-1, 1] is < the thresh. Otherwise,
|
2012-06-19 15:40:27 +00:00
|
|
|
// we allocate. We start allocate-biased and ping-pong to delete-biased
|
2016-03-31 13:13:22 +00:00
|
|
|
SkScalar delThresh = -SK_ScalarHalf;
|
2013-12-03 18:17:16 +00:00
|
|
|
const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
|
2012-06-19 15:40:27 +00:00
|
|
|
int s = 0;
|
|
|
|
|
|
|
|
int count = 0;
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++, ++s) {
|
2012-06-19 15:40:27 +00:00
|
|
|
if (kSwitchThreshPeriod == s) {
|
|
|
|
delThresh = -delThresh;
|
|
|
|
s = 0;
|
|
|
|
}
|
2016-03-31 13:13:22 +00:00
|
|
|
SkScalar del = r.nextSScalar1();
|
2012-08-23 18:09:54 +00:00
|
|
|
if (count &&
|
2012-06-19 15:40:27 +00:00
|
|
|
(kMaxObjects == count || del < delThresh)) {
|
|
|
|
delete objects[count-1];
|
|
|
|
--count;
|
|
|
|
} else {
|
|
|
|
objects[count] = new A;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
delete objects[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
2014-02-26 23:01:57 +00:00
|
|
|
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 GrMemoryPool gBenchPool;
|
|
|
|
};
|
|
|
|
GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
|
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
/**
|
|
|
|
* This benchmark creates objects and deletes them in random order
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrMemoryPoolBenchRandom : public Benchmark {
|
2012-06-19 15:40:27 +00:00
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
protected:
|
2015-07-13 13:18:39 +00:00
|
|
|
const char* onGetName() override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return "grmemorypool_random";
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom r;
|
2012-06-19 15:40:27 +00:00
|
|
|
enum {
|
|
|
|
kMaxObjects = 4 * (1 << 10),
|
|
|
|
};
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<B> objects[kMaxObjects];
|
2012-06-19 15:40:27 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2012-06-19 15:40:27 +00:00
|
|
|
uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == objects[idx].get()) {
|
2014-02-26 23:01:57 +00:00
|
|
|
objects[idx].reset(new B);
|
2012-06-19 15:40:27 +00:00
|
|
|
} else {
|
2016-03-17 17:51:27 +00:00
|
|
|
objects[idx].reset();
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
2014-02-26 23:01:57 +00:00
|
|
|
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); } }
|
|
|
|
#endif
|
|
|
|
static GrMemoryPool gBenchPool;
|
|
|
|
};
|
|
|
|
GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
|
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
/**
|
|
|
|
* This benchmark creates objects and deletes them in queue order
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrMemoryPoolBenchQueue : public Benchmark {
|
2012-06-19 15:40:27 +00:00
|
|
|
enum {
|
2013-09-10 19:23:38 +00:00
|
|
|
M = 4 * (1 << 10),
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
protected:
|
2015-07-13 13:18:39 +00:00
|
|
|
const char* onGetName() override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return "grmemorypool_queue";
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom r;
|
2014-02-26 23:01:57 +00:00
|
|
|
C* objects[M];
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2012-06-19 15:40:27 +00:00
|
|
|
uint32_t count = r.nextRangeU(0, M-1);
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2014-02-26 23:01:57 +00:00
|
|
|
objects[i] = new C;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
delete objects[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new GrMemoryPoolBenchStack(); )
|
|
|
|
DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
|
|
|
|
DEF_BENCH( return new GrMemoryPoolBenchQueue(); )
|