aboutsummaryrefslogtreecommitdiff
path: root/bench/GrMemoryPoolBench.cpp
blob: 9547171deddd7f7ef87080510b69077c6a90610b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * 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 "bench/Benchmark.h"
#include "include/private/GrTypesPriv.h"
#include "include/utils/SkRandom.h"
#include "src/gpu/GrMemoryPool.h"

#include <type_traits>

namespace {

// sizeof is a multiple of GrMemoryPool::kAlignment for 4, 8, or 16 byte alignment
struct alignas(GrMemoryPool::kAlignment) Aligned {
    char buf[32];
};
static_assert(sizeof(Aligned) == 32);
static_assert(sizeof(Aligned) % GrMemoryPool::kAlignment == 0);

// sizeof is not a multiple of GrMemoryPool::kAlignment (will not be a multiple of max_align_t
// if it's 4, 8, or 16, as desired).
struct alignas(2) Unaligned {
    char buf[30];
};
static_assert(sizeof(Unaligned) == 30);
static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0);

// When max_align_t == 16, 8, or 4 the padded Unaligned will also be 32
static_assert(SkAlignTo(sizeof(Unaligned), GrMemoryPool::kAlignment) == sizeof(Aligned));

// 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);

}  // namespace

// 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 {
                delete objs[j];
            }
        }

        // 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;
        }
        // Pop N objects off in FIFO order
        for (int j = 0; j < kMaxObjects; ++j) {
            if (pool) {
                pool->release(objs[j]);
            } else {
                delete objs[j];
            }
        }

        // Everything has been cleaned up for the next loop
    }
}

// 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];
            }
        }

        // 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 fName.c_str();
    }

    void onDraw(int loops, SkCanvas*) override {
        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);
    }

    SkString     fName;
    int          fPoolSize;
    RunBenchProc fProc;

    using INHERITED = Benchmark;
};

///////////////////////////////////////////////////////////////////////////////////////////////////

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); )