Skip to content

Commit 013d8b3

Browse files
committed
Benchmark for repeated large deallocations.
1 parent 6dbf08a commit 013d8b3

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <snmalloc/snmalloc.h>
2+
#include <test/measuretime.h>
3+
#include <test/setup.h>
4+
5+
using namespace snmalloc;
6+
7+
static constexpr size_t ALLOC_SIZE = 800 * 1024; // 800 KB
8+
static constexpr size_t ITERATIONS = 100000;
9+
10+
void test_alloc_dealloc_cycle()
11+
{
12+
{
13+
MeasureTime m;
14+
m << "Alloc/dealloc 800KB x " << ITERATIONS;
15+
16+
for (size_t i = 0; i < ITERATIONS; i++)
17+
{
18+
void* p = snmalloc::alloc(ALLOC_SIZE);
19+
SNMALLOC_CHECK(p != nullptr);
20+
snmalloc::dealloc(p);
21+
}
22+
}
23+
24+
snmalloc::debug_check_empty();
25+
}
26+
27+
void test_batch_alloc_then_dealloc()
28+
{
29+
static constexpr size_t BATCH = 128;
30+
31+
void* ptrs[BATCH];
32+
33+
MeasureTime m;
34+
m << "Batch alloc then dealloc 800KB x " << BATCH;
35+
for (size_t j = 0; j < ITERATIONS / BATCH; j++)
36+
{
37+
for (size_t i = 0; i < BATCH; i++)
38+
{
39+
ptrs[i] = snmalloc::alloc(ALLOC_SIZE);
40+
SNMALLOC_CHECK(ptrs[i] != nullptr);
41+
}
42+
43+
for (size_t i = 0; i < BATCH; i++)
44+
{
45+
snmalloc::dealloc(ptrs[i]);
46+
}
47+
}
48+
49+
snmalloc::debug_check_empty();
50+
}
51+
52+
void test_alloc_dealloc_with_touch()
53+
{
54+
{
55+
MeasureTime m;
56+
m << "Alloc/touch/dealloc 800KB x " << ITERATIONS;
57+
58+
for (size_t i = 0; i < ITERATIONS; i++)
59+
{
60+
char* p = static_cast<char*>(snmalloc::alloc(ALLOC_SIZE));
61+
SNMALLOC_CHECK(p != nullptr);
62+
// Touch every 4KiB and last bytes to ensure pages are faulted in
63+
for (size_t offset = 0; offset < ALLOC_SIZE; offset += 4096)
64+
{
65+
p[offset] = 1;
66+
}
67+
snmalloc::dealloc(p);
68+
}
69+
}
70+
71+
snmalloc::debug_check_empty();
72+
}
73+
74+
int main(int, char**)
75+
{
76+
setup();
77+
78+
test_alloc_dealloc_cycle();
79+
test_batch_alloc_then_dealloc();
80+
test_alloc_dealloc_with_touch();
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)