aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/GrMemoryPoolBench.cpp
diff options
context:
space:
mode:
authorGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-06-19 15:40:27 +0000
committerGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-06-19 15:40:27 +0000
commit4da34e36cb7a07c3a28ae2a135b1837c26fc7aea (patch)
tree36ba4b63893a5621fe1eb377ea7cb3f4692c42b3 /bench/GrMemoryPoolBench.cpp
parent50e4ce05521b0d95890e894691819882113aae86 (diff)
Add GrMemoryPool as a helper to override operators new/delete
Review URL: http://codereview.appspot.com/6306090/ git-svn-id: http://skia.googlecode.com/svn/trunk@4282 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'bench/GrMemoryPoolBench.cpp')
-rw-r--r--bench/GrMemoryPoolBench.cpp164
1 files changed, 164 insertions, 0 deletions
diff --git a/bench/GrMemoryPoolBench.cpp b/bench/GrMemoryPoolBench.cpp
new file mode 100644
index 0000000000..2fad7fcf4d
--- /dev/null
+++ b/bench/GrMemoryPoolBench.cpp
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "GrMemoryPool.h"
+#include "SkBenchmark.h"
+#include "SkRandom.h"
+#include "SkTScopedPtr.h"
+#include "SkTDArray.h"
+
+// change this to 0 to compare GrMemoryPool to default new / delete
+#define OVERRIDE_NEW 1
+
+namespace {
+struct A {
+ int gStuff[10];
+#if OVERRIDE_NEW
+ void* operator new (size_t size) { return gPool.allocate(size); }
+ void operator delete (void* mem) { if (mem) { return gPool.release(mem); } }
+#endif
+ static GrMemoryPool gPool;
+};
+GrMemoryPool A::gPool(10 * (1 << 10), 10 * (1 << 10));
+}
+
+
+/**
+ * This benchmark creates and deletes objects in stack order
+ */
+class GrMemoryPoolBenchStack : public SkBenchmark {
+ enum {
+ N = SkBENCHLOOP(5 * (1 << 20)),
+ };
+public:
+ GrMemoryPoolBenchStack(void* param) : INHERITED(param) {
+ }
+protected:
+ virtual const char* onGetName() {
+ return "grmemorypool_stack";
+ }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ SkRandom r;
+ enum {
+ kMaxObjects = 4 * (1 << 10),
+ };
+ A* objects[kMaxObjects];
+
+ // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise,
+ // we allocate. We start allocate-biased and ping-pong to delete-biased
+ SkFixed delThresh = -SK_FixedHalf;
+ enum {
+ kSwitchThreshPeriod = N / (2 * kMaxObjects),
+ };
+ int s = 0;
+
+ int count = 0;
+ for (int i = 0; i < N; i++, ++s) {
+ if (kSwitchThreshPeriod == s) {
+ delThresh = -delThresh;
+ s = 0;
+ }
+ SkFixed del = r.nextSFixed1();
+ if (count &&
+ (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:
+ typedef SkBenchmark INHERITED;
+};
+
+/**
+ * This benchmark creates objects and deletes them in random order
+ */
+class GrMemoryPoolBenchRandom : public SkBenchmark {
+ enum {
+ N = SkBENCHLOOP(5 * (1 << 20)),
+ };
+public:
+ GrMemoryPoolBenchRandom(void* param) : INHERITED(param) {
+ }
+protected:
+ virtual const char* onGetName() {
+ return "grmemorypool_random";
+ }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ SkRandom r;
+ enum {
+ kMaxObjects = 4 * (1 << 10),
+ };
+ SkTScopedPtr<A> objects[kMaxObjects];
+
+ for (int i = 0; i < N; i++) {
+ uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
+ if (NULL == objects[idx].get()) {
+ objects[idx].reset(new A);
+ } else {
+ objects[idx].reset(NULL);
+ }
+ }
+ }
+
+private:
+ typedef SkBenchmark INHERITED;
+};
+
+/**
+ * This benchmark creates objects and deletes them in queue order
+ */
+class GrMemoryPoolBenchQueue : public SkBenchmark {
+ enum {
+ N = SkBENCHLOOP((1 << 10)),
+ M = SkBENCHLOOP(4 * (1 << 10)),
+ };
+public:
+ GrMemoryPoolBenchQueue(void* param) : INHERITED(param) {
+ }
+protected:
+ virtual const char* onGetName() {
+ return "grmemorypool_queue";
+ }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ SkRandom r;
+ A* objects[M];
+ for (int i = 0; i < N; i++) {
+ uint32_t count = r.nextRangeU(0, M-1);
+ for (uint32_t i = 0; i < count; i++) {
+ objects[i] = new A;
+ }
+ for (uint32_t i = 0; i < count; i++) {
+ delete objects[i];
+ }
+ }
+ }
+
+private:
+ typedef SkBenchmark INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+static SkBenchmark* Fact1(void* p) { return new GrMemoryPoolBenchStack(p); }
+static SkBenchmark* Fact2(void* p) { return new GrMemoryPoolBenchRandom(p); }
+static SkBenchmark* Fact3(void* p) { return new GrMemoryPoolBenchQueue(p); }
+
+static BenchRegistry gReg01(Fact1);
+static BenchRegistry gReg02(Fact2);
+static BenchRegistry gReg03(Fact3);
+