forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_pool_benchmark.cc
More file actions
129 lines (108 loc) · 3.86 KB
/
Copy pathmemory_pool_benchmark.cc
File metadata and controls
129 lines (108 loc) · 3.86 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/memory_pool.h"
#include "arrow/result.h"
#include "arrow/util/logging.h"
#include "benchmark/benchmark.h"
namespace arrow {
struct SystemAlloc {
static Result<MemoryPool*> GetAllocator() { return system_memory_pool(); }
};
#ifdef ARROW_JEMALLOC
struct Jemalloc {
static Result<MemoryPool*> GetAllocator() {
MemoryPool* pool;
RETURN_NOT_OK(jemalloc_memory_pool(&pool));
return pool;
}
};
#endif
#ifdef ARROW_MIMALLOC
struct Mimalloc {
static Result<MemoryPool*> GetAllocator() {
MemoryPool* pool;
RETURN_NOT_OK(mimalloc_memory_pool(&pool));
return pool;
}
};
#endif
static void TouchCacheLines(uint8_t* data, int64_t nbytes) {
uint8_t total = 0;
while (nbytes > 0) {
total += *data;
data += 64;
nbytes -= 64;
}
benchmark::DoNotOptimize(total);
}
// Benchmark the cost of accessing always the same memory area.
// This gives us a lower bound of the potential difference between
// AllocateTouchDeallocate and AllocateDeallocate.
static void TouchArea(benchmark::State& state) { // NOLINT non-const reference
const int64_t nbytes = state.range(0);
MemoryPool* pool = default_memory_pool();
uint8_t* data;
ARROW_CHECK_OK(pool->Allocate(nbytes, &data));
for (auto _ : state) {
TouchCacheLines(data, nbytes);
}
pool->Free(data, nbytes);
}
// Benchmark the raw cost of allocating memory.
// Note this is a best case situation: we always allocate and deallocate exactly
// the same size, without any other allocator traffic. However, it can be
// representative of workloads where we routinely create and destroy
// temporary buffers for intermediate computation results.
template <typename Alloc>
static void AllocateDeallocate(benchmark::State& state) { // NOLINT non-const reference
const int64_t nbytes = state.range(0);
MemoryPool* pool = *Alloc::GetAllocator();
for (auto _ : state) {
uint8_t* data;
ARROW_CHECK_OK(pool->Allocate(nbytes, &data));
pool->Free(data, nbytes);
}
}
// Benchmark the cost of allocating memory plus accessing it.
template <typename Alloc>
static void AllocateTouchDeallocate(
benchmark::State& state) { // NOLINT non-const reference
const int64_t nbytes = state.range(0);
MemoryPool* pool = *Alloc::GetAllocator();
for (auto _ : state) {
uint8_t* data;
ARROW_CHECK_OK(pool->Allocate(nbytes, &data));
TouchCacheLines(data, nbytes);
pool->Free(data, nbytes);
}
}
#define BENCHMARK_ALLOCATE_ARGS \
->RangeMultiplier(16)->Range(4096, 16 * 1024 * 1024)->ArgName("size")->UseRealTime()
#define BENCHMARK_ALLOCATE(benchmark_func, template_param) \
BENCHMARK_TEMPLATE(benchmark_func, template_param) BENCHMARK_ALLOCATE_ARGS
BENCHMARK(TouchArea) BENCHMARK_ALLOCATE_ARGS;
BENCHMARK_ALLOCATE(AllocateDeallocate, SystemAlloc);
BENCHMARK_ALLOCATE(AllocateTouchDeallocate, SystemAlloc);
#ifdef ARROW_JEMALLOC
BENCHMARK_ALLOCATE(AllocateDeallocate, Jemalloc);
BENCHMARK_ALLOCATE(AllocateTouchDeallocate, Jemalloc);
#endif
#ifdef ARROW_MIMALLOC
BENCHMARK_ALLOCATE(AllocateDeallocate, Mimalloc);
BENCHMARK_ALLOCATE(AllocateTouchDeallocate, Mimalloc);
#endif
} // namespace arrow