Skip to content

Commit f11402a

Browse files
authored
Benchmark: use not_highly_aligned_allocator in more places (#5443)
1 parent 1e56660 commit f11402a

File tree

8 files changed

+48
-29
lines changed

8 files changed

+48
-29
lines changed

benchmarks/src/adjacent_difference.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <type_traits>
1212
#include <vector>
1313

14+
#include "skewed_allocator.hpp"
15+
1416
using namespace std;
1517

1618
template <class T>
@@ -19,8 +21,8 @@ void bm(benchmark::State& state) {
1921

2022
const size_t size = static_cast<size_t>(state.range(0));
2123

22-
vector<T> input(size);
23-
vector<T> output(size);
24+
vector<T, not_highly_aligned_allocator<T>> input(size);
25+
vector<T, not_highly_aligned_allocator<T>> output(size);
2426

2527
if constexpr (is_floating_point_v<T>) {
2628
normal_distribution<T> dis(0, 100000.0);

benchmarks/src/adjacent_find.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <cstdlib>
99
#include <vector>
1010

11+
#include "skewed_allocator.hpp"
12+
1113
using namespace std;
1214

1315
enum class AlgType { Std, Rng };
@@ -17,7 +19,7 @@ void bm(benchmark::State& state) {
1719
const size_t size = static_cast<size_t>(state.range(0));
1820
const size_t pos = static_cast<size_t>(state.range(1));
1921

20-
vector<T> v(size);
22+
vector<T, not_highly_aligned_allocator<T>> v(size);
2123

2224
for (size_t i = 0; i != size; ++i) {
2325
v[i] = static_cast<T>(i & 3);

benchmarks/src/iota.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <numeric>
88
#include <vector>
99

10+
#include "skewed_allocator.hpp"
11+
1012
enum class Alg {
1113
Std,
1214
Rng,
@@ -16,7 +18,7 @@ template <class T, Alg Algorithm>
1618
void bm(benchmark::State& state) {
1719
const auto size = static_cast<std::size_t>(state.range(0));
1820

19-
std::vector<T> a(size);
21+
std::vector<T, not_highly_aligned_allocator<T>> a(size);
2022

2123
for (auto _ : state) {
2224
if constexpr (Algorithm == Alg::Std) {

benchmarks/src/minmax_element.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <type_traits>
1111
#include <vector>
1212

13+
#include "skewed_allocator.hpp"
14+
1315
enum class Op {
1416
Min,
1517
Max,
@@ -23,7 +25,7 @@ using namespace std;
2325

2426
template <class T, Op Operation>
2527
void bm(benchmark::State& state) {
26-
vector<T> a(static_cast<size_t>(state.range()));
28+
vector<T, not_highly_aligned_allocator<T>> a(static_cast<size_t>(state.range()));
2729

2830
mt19937 gen(84710);
2931

benchmarks/src/mismatch.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <ranges>
99
#include <vector>
1010

11+
#include "skewed_allocator.hpp"
12+
1113
using namespace std;
1214

1315
constexpr int64_t no_pos = -1;
@@ -19,8 +21,8 @@ enum class op {
1921

2022
template <class T, op Op>
2123
void bm(benchmark::State& state) {
22-
vector<T> a(static_cast<size_t>(state.range(0)), T{'.'});
23-
vector<T> b(static_cast<size_t>(state.range(0)), T{'.'});
24+
vector<T, not_highly_aligned_allocator<T>> a(static_cast<size_t>(state.range(0)), T{'.'});
25+
vector<T, not_highly_aligned_allocator<T>> b(static_cast<size_t>(state.range(0)), T{'.'});
2426

2527
if (state.range(1) != no_pos) {
2628
b.at(static_cast<size_t>(state.range(1))) = 'x';

benchmarks/src/remove.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
#include <vector>
88

99
#include "lorem.hpp"
10+
#include "skewed_allocator.hpp"
1011

1112
enum class alg_type { std_fn, rng };
1213

1314
template <alg_type Type, class T>
1415
void r(benchmark::State& state) {
15-
const std::vector<T> src(lorem_ipsum.begin(), lorem_ipsum.end());
16-
std::vector<T> v;
16+
const std::vector<T, not_highly_aligned_allocator<T>> src(lorem_ipsum.begin(), lorem_ipsum.end());
17+
std::vector<T, not_highly_aligned_allocator<T>> v;
1718
v.reserve(lorem_ipsum.size());
1819
for (auto _ : state) {
1920
v = src;
@@ -28,8 +29,8 @@ void r(benchmark::State& state) {
2829

2930
template <alg_type Type, class T>
3031
void rc(benchmark::State& state) {
31-
std::vector<T> src(lorem_ipsum.begin(), lorem_ipsum.end());
32-
std::vector<T> v(lorem_ipsum.size());
32+
std::vector<T, not_highly_aligned_allocator<T>> src(lorem_ipsum.begin(), lorem_ipsum.end());
33+
std::vector<T, not_highly_aligned_allocator<T>> v(lorem_ipsum.size());
3334
for (auto _ : state) {
3435
benchmark::DoNotOptimize(src);
3536
benchmark::DoNotOptimize(v);

benchmarks/src/replace.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,43 @@
77
#include <vector>
88

99
#include "lorem.hpp"
10+
#include "skewed_allocator.hpp"
1011

1112
template <class T>
1213
void r(benchmark::State& state) {
13-
const std::vector<T> a(lorem_ipsum.begin(), lorem_ipsum.end());
14-
std::vector<T> b(lorem_ipsum.size());
14+
std::vector<T, not_highly_aligned_allocator<T>> a(lorem_ipsum.begin(), lorem_ipsum.end());
15+
std::vector<T, not_highly_aligned_allocator<T>> b(lorem_ipsum.size());
1516

1617
for (auto _ : state) {
18+
benchmark::DoNotOptimize(a);
1719
b = a;
1820
std::replace(std::begin(b), std::end(b), T{'m'}, T{'w'});
21+
benchmark::DoNotOptimize(b);
1922
}
2023
}
2124

2225
template <class T>
2326
void rc(benchmark::State& state) {
24-
const std::vector<T> a(lorem_ipsum.begin(), lorem_ipsum.end());
25-
std::vector<T> b(lorem_ipsum.size());
27+
std::vector<T, not_highly_aligned_allocator<T>> a(lorem_ipsum.begin(), lorem_ipsum.end());
28+
std::vector<T, not_highly_aligned_allocator<T>> b(lorem_ipsum.size());
2629

2730
for (auto _ : state) {
31+
benchmark::DoNotOptimize(a);
2832
std::replace_copy(std::begin(a), std::end(a), std::begin(b), T{'m'}, T{'w'});
33+
benchmark::DoNotOptimize(b);
2934
}
3035
}
3136

3237
template <class T>
3338
void rc_if(benchmark::State& state) {
34-
const std::vector<T> a(lorem_ipsum.begin(), lorem_ipsum.end());
35-
std::vector<T> b(lorem_ipsum.size());
39+
std::vector<T, not_highly_aligned_allocator<T>> a(lorem_ipsum.begin(), lorem_ipsum.end());
40+
std::vector<T, not_highly_aligned_allocator<T>> b(lorem_ipsum.size());
3641

3742
for (auto _ : state) {
43+
benchmark::DoNotOptimize(a);
3844
(void) std::replace_copy_if(
3945
std::begin(a), std::end(a), std::begin(b), [](auto x) { return x <= T{'Z'}; }, T{'X'});
46+
benchmark::DoNotOptimize(b);
4047
}
4148
}
4249

benchmarks/src/std_copy.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include <type_traits>
99
#include <vector>
1010

11-
#include <udt.hpp>
12-
#include <utility.hpp>
11+
#include "skewed_allocator.hpp"
12+
#include "udt.hpp"
13+
#include "utility.hpp"
1314

1415
template <typename Contained>
1516
void handwritten_loop(benchmark::State& state) {
1617
const size_t r0 = static_cast<size_t>(state.range(0));
17-
const auto in_buffer = random_vector<Contained>(r0);
18-
std::vector<Contained> out_buffer(r0);
18+
const auto in_buffer = random_vector<Contained, not_highly_aligned_allocator>(r0);
19+
std::vector<Contained, not_highly_aligned_allocator<Contained>> out_buffer(r0);
1920
for ([[maybe_unused]] auto _ : state) {
2021
benchmark::DoNotOptimize(in_buffer.data());
2122
const Contained* in_ptr = in_buffer.data();
@@ -32,8 +33,8 @@ void handwritten_loop(benchmark::State& state) {
3233
template <typename Contained>
3334
void handwritten_loop_n(benchmark::State& state) {
3435
const size_t r0 = static_cast<size_t>(state.range(0));
35-
const auto in_buffer = random_vector<Contained>(r0);
36-
std::vector<Contained> out_buffer(r0);
36+
const auto in_buffer = random_vector<Contained, not_highly_aligned_allocator>(r0);
37+
std::vector<Contained, not_highly_aligned_allocator<Contained>> out_buffer(r0);
3738
for ([[maybe_unused]] auto _ : state) {
3839
benchmark::DoNotOptimize(in_buffer.data());
3940
const Contained* const in_ptr = in_buffer.data();
@@ -50,8 +51,8 @@ template <typename Contained>
5051
void memcpy_call(benchmark::State& state) {
5152
static_assert(std::is_trivially_copyable_v<Contained>, "memcpy must only be called on trivially copyable types");
5253
const size_t r0 = static_cast<size_t>(state.range(0));
53-
const auto in_buffer = random_vector<Contained>(r0);
54-
std::vector<Contained> out_buffer(r0);
54+
const auto in_buffer = random_vector<Contained, not_highly_aligned_allocator>(r0);
55+
std::vector<Contained, not_highly_aligned_allocator<Contained>> out_buffer(r0);
5556
for ([[maybe_unused]] auto _ : state) {
5657
benchmark::DoNotOptimize(in_buffer.data());
5758
memcpy(out_buffer.data(), in_buffer.data(), r0 * sizeof(Contained));
@@ -62,8 +63,8 @@ void memcpy_call(benchmark::State& state) {
6263
template <typename Contained>
6364
void std_copy_call(benchmark::State& state) {
6465
const size_t r0 = static_cast<size_t>(state.range(0));
65-
const auto in_buffer = random_vector<Contained>(r0);
66-
std::vector<Contained> out_buffer(r0);
66+
const auto in_buffer = random_vector<Contained, not_highly_aligned_allocator>(r0);
67+
std::vector<Contained, not_highly_aligned_allocator<Contained>> out_buffer(r0);
6768
for ([[maybe_unused]] auto _ : state) {
6869
benchmark::DoNotOptimize(in_buffer.data());
6970
std::copy(in_buffer.begin(), in_buffer.end(), out_buffer.begin());
@@ -74,8 +75,8 @@ void std_copy_call(benchmark::State& state) {
7475
template <typename Contained>
7576
void std_copy_n_call(benchmark::State& state) {
7677
const size_t r0 = static_cast<size_t>(state.range(0));
77-
const auto in_buffer = random_vector<Contained>(r0);
78-
std::vector<Contained> out_buffer(r0);
78+
const auto in_buffer = random_vector<Contained, not_highly_aligned_allocator>(r0);
79+
std::vector<Contained, not_highly_aligned_allocator<Contained>> out_buffer(r0);
7980
for ([[maybe_unused]] auto _ : state) {
8081
benchmark::DoNotOptimize(in_buffer.data());
8182
std::copy_n(in_buffer.begin(), r0, out_buffer.begin());

0 commit comments

Comments
 (0)