Skip to content

Commit d2f8843

Browse files
committed
add benchmarks
1 parent a3c9680 commit d2f8843

File tree

4 files changed

+143
-53
lines changed

4 files changed

+143
-53
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ out/
1010
build/*
1111
!build/*.sh
1212
*.exe
13+
14+
.cache/

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include(GNUInstallDirs)
66
include(CMakePackageConfigHelpers)
77

88
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build")
9+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
910

1011
if(NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|Release|RelWithDebInfo)$")
1112
message(WARNING "The specified build type [${CMAKE_BUILD_TYPE}] is not recognized. Defaulting to Release.")

benchmark/small_unique_ptr.cpp

Lines changed: 140 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,70 +18,184 @@ struct LargeDerived : public Base
1818
/* --------------------------------------------------------------------------------------------- */
1919

2020
template<typename T>
21-
void benchmark_make_unique(benchmark::State& state)
21+
void bm_make_unique(benchmark::State& state)
2222
{
2323
for (auto _ : state)
2424
{
2525
std::unique_ptr<Base> ptr = std::make_unique<T>();
2626
benchmark::DoNotOptimize(ptr);
27-
benchmark::ClobberMemory();
2827
}
2928
}
3029

3130

3231
template<typename T>
33-
void benchmark_make_unique_small(benchmark::State& state)
32+
void bm_make_unique_small(benchmark::State& state)
3433
{
3534
for (auto _ : state)
3635
{
3736
smp::small_unique_ptr<Base> ptr = smp::make_unique_small<T, Base>();
3837
benchmark::DoNotOptimize(ptr);
39-
benchmark::ClobberMemory();
4038
}
4139
}
4240

43-
BENCHMARK(benchmark_make_unique<SmallDerived>);
44-
BENCHMARK(benchmark_make_unique_small<SmallDerived>);
45-
BENCHMARK(benchmark_make_unique<LargeDerived>);
46-
BENCHMARK(benchmark_make_unique_small<LargeDerived>);
41+
template<typename T>
42+
void bm_make_unique_small_cast(benchmark::State& state)
43+
{
44+
for (auto _ : state)
45+
{
46+
smp::small_unique_ptr<Base> ptr = smp::make_unique_small<T>();
47+
benchmark::DoNotOptimize(ptr);
48+
}
49+
}
50+
51+
BENCHMARK(bm_make_unique<SmallDerived>);
52+
BENCHMARK(bm_make_unique_small<SmallDerived>);
53+
BENCHMARK(bm_make_unique_small_cast<SmallDerived>);
54+
55+
BENCHMARK(bm_make_unique<LargeDerived>);
56+
BENCHMARK(bm_make_unique_small<LargeDerived>);
57+
BENCHMARK(bm_make_unique_small_cast<LargeDerived>);
4758

4859
/* --------------------------------------------------------------------------------------------- */
4960

5061
template<typename T>
51-
void benchmark_swap_unique_ptr(benchmark::State& state)
62+
void bm_move_construct2_unique_ptr(benchmark::State& state)
5263
{
53-
std::unique_ptr<T> left = std::make_unique<T>();
54-
std::unique_ptr<T> right = std::make_unique<T>();
64+
std::unique_ptr<Base> p1 = std::make_unique<T>();
65+
std::unique_ptr<Base> p2;
5566

5667
for (auto _ : state)
5768
{
58-
using std::swap;
59-
swap(left, right);
69+
std::construct_at(&p2, std::move(p1));
70+
std::destroy_at(&p1);
71+
72+
benchmark::DoNotOptimize(p1);
73+
benchmark::DoNotOptimize(p2);
74+
75+
std::construct_at(&p1, std::move(p2));
76+
std::destroy_at(&p2);
77+
78+
benchmark::DoNotOptimize(p1);
79+
benchmark::DoNotOptimize(p2);
80+
}
81+
}
82+
83+
template<typename T>
84+
void bm_move_construct2_small_unique_ptr(benchmark::State& state)
85+
{
86+
smp::small_unique_ptr<Base> p1 = smp::make_unique_small<T>();
87+
smp::small_unique_ptr<Base> p2;
88+
89+
for (auto _ : state)
90+
{
91+
std::construct_at(&p2, std::move(p1));
92+
std::destroy_at(&p1);
93+
94+
benchmark::DoNotOptimize(p1);
95+
benchmark::DoNotOptimize(p2);
96+
97+
std::construct_at(&p1, std::move(p2));
98+
std::destroy_at(&p2);
99+
100+
benchmark::DoNotOptimize(p1);
101+
benchmark::DoNotOptimize(p2);
102+
}
103+
}
104+
105+
BENCHMARK(bm_move_construct2_unique_ptr<SmallDerived>);
106+
BENCHMARK(bm_move_construct2_unique_ptr<LargeDerived>);
107+
108+
BENCHMARK(bm_move_construct2_small_unique_ptr<SmallDerived>);
109+
BENCHMARK(bm_move_construct2_small_unique_ptr<LargeDerived>);
110+
111+
/* --------------------------------------------------------------------------------------------- */
112+
113+
template<typename T>
114+
void bm_move_assign2_unique_ptr(benchmark::State& state)
115+
{
116+
std::unique_ptr<Base> left = std::make_unique<T>();
117+
std::unique_ptr<Base> right = std::make_unique<T>();
118+
119+
for (auto _ : state)
120+
{
121+
right = std::move(left);
122+
123+
benchmark::DoNotOptimize(left);
124+
benchmark::DoNotOptimize(right);
125+
126+
left = std::move(right);
127+
60128
benchmark::DoNotOptimize(left);
61129
benchmark::DoNotOptimize(right);
62-
benchmark::ClobberMemory();
63130
}
64131
}
65132

66-
67-
template<typename T, typename U = T>
68-
void benchmark_swap_small_unique_ptr(benchmark::State& state)
133+
template<typename T>
134+
void bm_move_assign2_small_unique_ptr(benchmark::State& state)
69135
{
70136
smp::small_unique_ptr<Base> left = smp::make_unique_small<T>();
71-
smp::small_unique_ptr<Base> right = smp::make_unique_small<U>();
137+
smp::small_unique_ptr<Base> right = smp::make_unique_small<T>();
72138

73139
for (auto _ : state)
74140
{
75-
using std::swap;
76-
swap(left, right);
141+
right = std::move(left);
142+
143+
benchmark::DoNotOptimize(left);
144+
benchmark::DoNotOptimize(right);
145+
146+
left = std::move(right);
147+
77148
benchmark::DoNotOptimize(left);
78149
benchmark::DoNotOptimize(right);
79-
benchmark::ClobberMemory();
80150
}
81151
}
82152

83-
BENCHMARK(benchmark_swap_unique_ptr<SmallDerived>);
84-
BENCHMARK(benchmark_swap_unique_ptr<LargeDerived>);
85-
BENCHMARK(benchmark_swap_small_unique_ptr<SmallDerived, SmallDerived>);
86-
BENCHMARK(benchmark_swap_small_unique_ptr<LargeDerived, LargeDerived>);
87-
BENCHMARK(benchmark_swap_small_unique_ptr<SmallDerived, LargeDerived>);
153+
BENCHMARK(bm_move_assign2_unique_ptr<SmallDerived>);
154+
BENCHMARK(bm_move_assign2_unique_ptr<LargeDerived>);
155+
156+
BENCHMARK(bm_move_assign2_small_unique_ptr<SmallDerived>);
157+
BENCHMARK(bm_move_assign2_small_unique_ptr<LargeDerived>);
158+
159+
/* --------------------------------------------------------------------------------------------- */
160+
161+
template<typename T>
162+
void bm_swap_unique_ptr(benchmark::State& state)
163+
{
164+
std::unique_ptr<T> lhs = std::make_unique<T>();
165+
std::unique_ptr<T> rhs = std::make_unique<T>();
166+
167+
for (auto _ : state)
168+
{
169+
benchmark::DoNotOptimize(lhs);
170+
benchmark::DoNotOptimize(rhs);
171+
172+
using std::swap;
173+
swap(lhs, rhs);
174+
}
175+
}
176+
177+
178+
template<typename T, typename U = T>
179+
void bm_swap_small_unique_ptr(benchmark::State& state)
180+
{
181+
smp::small_unique_ptr<Base> lhs = smp::make_unique_small<T>();
182+
smp::small_unique_ptr<Base> rhs = smp::make_unique_small<U>();
183+
184+
for (auto _ : state)
185+
{
186+
benchmark::DoNotOptimize(lhs);
187+
benchmark::DoNotOptimize(rhs);
188+
189+
using std::swap;
190+
swap(lhs, rhs);
191+
}
192+
}
193+
194+
BENCHMARK(bm_swap_unique_ptr<SmallDerived>);
195+
BENCHMARK(bm_swap_unique_ptr<LargeDerived>);
196+
197+
BENCHMARK(bm_swap_small_unique_ptr<SmallDerived, SmallDerived>);
198+
BENCHMARK(bm_swap_small_unique_ptr<LargeDerived, LargeDerived>);
199+
BENCHMARK(bm_swap_small_unique_ptr<SmallDerived, LargeDerived>);
200+
201+
/* --------------------------------------------------------------------------------------------- */

build/install_catch.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)