Skip to content

Commit d672c6d

Browse files
committed
Fix uses of ClobberMemory
1 parent b212204 commit d672c6d

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed

libcxx/test/benchmarks/algorithms/modifying/copy.bench.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// UNSUPPORTED: c++03, c++11, c++14, c++17
1010

1111
#include <algorithm>
12+
#include <cstddef>
1213
#include <deque>
1314
#include <iterator>
1415
#include <list>
@@ -22,19 +23,18 @@
2223
template <class Container, class Operation>
2324
void bm_general(std::string operation_name, Operation copy) {
2425
auto bench = [copy](auto& st) {
25-
auto const size = st.range(0);
26-
using ValueType = typename Container::value_type;
26+
std::size_t const n = st.range(0);
27+
using ValueType = typename Container::value_type;
2728
Container c;
28-
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
29+
std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); });
2930

30-
std::vector<ValueType> out(size);
31+
std::vector<ValueType> out(n);
3132

3233
for ([[maybe_unused]] auto _ : st) {
34+
benchmark::DoNotOptimize(c);
35+
benchmark::DoNotOptimize(out);
3336
auto result = copy(c.begin(), c.end(), out.begin());
3437
benchmark::DoNotOptimize(result);
35-
benchmark::DoNotOptimize(out);
36-
benchmark::DoNotOptimize(c);
37-
benchmark::ClobberMemory();
3838
}
3939
};
4040
benchmark::RegisterBenchmark(operation_name, bench)->Range(8, 1 << 20);
@@ -43,18 +43,17 @@ void bm_general(std::string operation_name, Operation copy) {
4343
template <bool Aligned, class Operation>
4444
static void bm_vector_bool(std::string operation_name, Operation copy) {
4545
auto bench = [copy](auto& st) {
46-
auto n = st.range();
46+
std::size_t const n = st.range(0);
4747
std::vector<bool> in(n, true);
4848
std::vector<bool> out(Aligned ? n : n + 8);
49-
benchmark::DoNotOptimize(&in);
5049
auto first = in.begin();
5150
auto last = in.end();
5251
auto dst = Aligned ? out.begin() : out.begin() + 4;
5352
for ([[maybe_unused]] auto _ : st) {
53+
benchmark::DoNotOptimize(in);
54+
benchmark::DoNotOptimize(out);
5455
auto result = copy(first, last, dst);
5556
benchmark::DoNotOptimize(result);
56-
benchmark::DoNotOptimize(out);
57-
benchmark::ClobberMemory();
5857
}
5958
};
6059
benchmark::RegisterBenchmark(operation_name, bench)->Range(64, 1 << 20);

libcxx/test/benchmarks/algorithms/modifying/copy_backward.bench.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// UNSUPPORTED: c++03, c++11, c++14, c++17
1010

1111
#include <algorithm>
12+
#include <cstddef>
1213
#include <deque>
1314
#include <iterator>
1415
#include <list>
@@ -22,19 +23,18 @@
2223
template <class Container, class Operation>
2324
void bm_general(std::string operation_name, Operation copy_backward) {
2425
auto bench = [copy_backward](auto& st) {
25-
auto const size = st.range(0);
26-
using ValueType = typename Container::value_type;
26+
std::size_t const n = st.range(0);
27+
using ValueType = typename Container::value_type;
2728
Container c;
28-
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
29+
std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); });
2930

30-
std::vector<ValueType> out(size);
31+
std::vector<ValueType> out(n);
3132

3233
for ([[maybe_unused]] auto _ : st) {
34+
benchmark::DoNotOptimize(c);
35+
benchmark::DoNotOptimize(out);
3336
auto result = copy_backward(c.begin(), c.end(), out.end());
3437
benchmark::DoNotOptimize(result);
35-
benchmark::DoNotOptimize(out);
36-
benchmark::DoNotOptimize(c);
37-
benchmark::ClobberMemory();
3838
}
3939
};
4040
benchmark::RegisterBenchmark(operation_name, bench)->Range(8, 1 << 20);
@@ -43,18 +43,18 @@ void bm_general(std::string operation_name, Operation copy_backward) {
4343
template <bool Aligned, class Operation>
4444
static void bm_vector_bool(std::string operation_name, Operation copy_backward) {
4545
auto bench = [copy_backward](auto& st) {
46-
auto n = st.range();
46+
std::size_t const n = st.range(0);
4747
std::vector<bool> in(n, true);
4848
std::vector<bool> out(Aligned ? n : n + 8);
4949
benchmark::DoNotOptimize(&in);
5050
auto first = in.begin();
5151
auto last = in.end();
5252
auto dst = Aligned ? out.end() : out.end() - 4;
5353
for ([[maybe_unused]] auto _ : st) {
54+
benchmark::DoNotOptimize(in);
55+
benchmark::DoNotOptimize(out);
5456
auto result = copy_backward(first, last, dst);
5557
benchmark::DoNotOptimize(result);
56-
benchmark::DoNotOptimize(out);
57-
benchmark::ClobberMemory();
5858
}
5959
};
6060
benchmark::RegisterBenchmark(operation_name, bench)->Range(64, 1 << 20);

libcxx/test/benchmarks/algorithms/modifying/copy_if.bench.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// UNSUPPORTED: c++03, c++11, c++14, c++17
1010

1111
#include <algorithm>
12+
#include <cstddef>
1213
#include <deque>
1314
#include <iterator>
1415
#include <list>
@@ -24,12 +25,12 @@
2425
template <class Container, class Operation>
2526
void bm_copy_every_other_element(std::string operation_name, Operation copy_if) {
2627
auto bench = [copy_if](auto& st) {
27-
auto const size = st.range(0);
28-
using ValueType = typename Container::value_type;
28+
std::size_t const n = st.range(0);
29+
using ValueType = typename Container::value_type;
2930
Container c;
30-
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
31+
std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); });
3132

32-
std::vector<ValueType> out(size);
33+
std::vector<ValueType> out(n);
3334

3435
for ([[maybe_unused]] auto _ : st) {
3536
bool do_copy = false;
@@ -38,11 +39,10 @@ void bm_copy_every_other_element(std::string operation_name, Operation copy_if)
3839
do_copy = !do_copy;
3940
return do_copy;
4041
};
42+
benchmark::DoNotOptimize(c);
43+
benchmark::DoNotOptimize(out);
4144
auto result = copy_if(c.begin(), c.end(), out.begin(), pred);
4245
benchmark::DoNotOptimize(result);
43-
benchmark::DoNotOptimize(out);
44-
benchmark::DoNotOptimize(c);
45-
benchmark::ClobberMemory();
4646
}
4747
};
4848
benchmark::RegisterBenchmark(operation_name, bench)->Range(8, 1 << 20);
@@ -52,23 +52,22 @@ void bm_copy_every_other_element(std::string operation_name, Operation copy_if)
5252
template <class Container, class Operation>
5353
void bm_copy_entire_range(std::string operation_name, Operation copy_if) {
5454
auto bench = [copy_if](auto& st) {
55-
auto const size = st.range(0);
56-
using ValueType = typename Container::value_type;
55+
std::size_t const n = st.range(0);
56+
using ValueType = typename Container::value_type;
5757
Container c;
58-
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
58+
std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); });
5959

60-
std::vector<ValueType> out(size);
60+
std::vector<ValueType> out(n);
6161

6262
for ([[maybe_unused]] auto _ : st) {
6363
auto pred = [](auto& element) {
6464
benchmark::DoNotOptimize(element);
6565
return true;
6666
};
67+
benchmark::DoNotOptimize(c);
68+
benchmark::DoNotOptimize(out);
6769
auto result = copy_if(c.begin(), c.end(), out.begin(), pred);
6870
benchmark::DoNotOptimize(result);
69-
benchmark::DoNotOptimize(out);
70-
benchmark::DoNotOptimize(c);
71-
benchmark::ClobberMemory();
7271
}
7372
};
7473
benchmark::RegisterBenchmark(operation_name, bench)->Range(8, 1 << 20);

libcxx/test/benchmarks/algorithms/modifying/copy_n.bench.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// UNSUPPORTED: c++03, c++11, c++14, c++17
1010

1111
#include <algorithm>
12+
#include <cstddef>
1213
#include <deque>
1314
#include <iterator>
1415
#include <list>
@@ -22,19 +23,18 @@
2223
template <class Container, class Operation>
2324
void bm_general(std::string operation_name, Operation copy_n) {
2425
auto bench = [copy_n](auto& st) {
25-
auto const size = st.range(0);
26-
using ValueType = typename Container::value_type;
26+
std::size_t const n = st.range(0);
27+
using ValueType = typename Container::value_type;
2728
Container c;
28-
std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });
29+
std::generate_n(std::back_inserter(c), n, [] { return Generate<ValueType>::random(); });
2930

30-
std::vector<ValueType> out(size);
31+
std::vector<ValueType> out(n);
3132

3233
for ([[maybe_unused]] auto _ : st) {
33-
auto result = copy_n(c.begin(), size, out.begin());
34-
benchmark::DoNotOptimize(result);
35-
benchmark::DoNotOptimize(out);
3634
benchmark::DoNotOptimize(c);
37-
benchmark::ClobberMemory();
35+
benchmark::DoNotOptimize(out);
36+
auto result = copy_n(c.begin(), n, out.begin());
37+
benchmark::DoNotOptimize(result);
3838
}
3939
};
4040
benchmark::RegisterBenchmark(operation_name, bench)->Range(8, 1 << 20);
@@ -43,17 +43,16 @@ void bm_general(std::string operation_name, Operation copy_n) {
4343
template <bool Aligned, class Operation>
4444
static void bm_vector_bool(std::string operation_name, Operation copy_n) {
4545
auto bench = [copy_n](auto& st) {
46-
auto n = st.range();
46+
std::size_t const n = st.range(0);
4747
std::vector<bool> in(n, true);
4848
std::vector<bool> out(Aligned ? n : n + 8);
49-
benchmark::DoNotOptimize(&in);
5049
auto first = in.begin();
5150
auto dst = Aligned ? out.begin() : out.begin() + 4;
5251
for ([[maybe_unused]] auto _ : st) {
52+
benchmark::DoNotOptimize(in);
53+
benchmark::DoNotOptimize(out);
5354
auto result = copy_n(first, n, dst);
5455
benchmark::DoNotOptimize(result);
55-
benchmark::DoNotOptimize(out);
56-
benchmark::ClobberMemory();
5756
}
5857
};
5958
benchmark::RegisterBenchmark(operation_name, bench)->Range(64, 1 << 20);

0 commit comments

Comments
 (0)