Skip to content

Commit 55c1ebe

Browse files
committed
Fix DoNotOptimize
1 parent 9fede1f commit 55c1ebe

27 files changed

+113
-149
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ int main(int argc, char** argv) {
3636
Container c(size, y);
3737

3838
for ([[maybe_unused]] auto _ : st) {
39-
fill(c.begin(), c.end(), x);
40-
std::swap(x, y);
4139
benchmark::DoNotOptimize(c);
4240
benchmark::DoNotOptimize(x);
43-
benchmark::DoNotOptimize(y);
44-
benchmark::ClobberMemory();
41+
fill(c.begin(), c.end(), x);
42+
benchmark::DoNotOptimize(c);
43+
std::swap(x, y);
4544
}
4645
})
4746
->Arg(32)
@@ -66,12 +65,11 @@ int main(int argc, char** argv) {
6665
std::vector<bool> c(size, y);
6766

6867
for ([[maybe_unused]] auto _ : st) {
69-
fill(c.begin(), c.end(), x);
70-
std::swap(x, y);
7168
benchmark::DoNotOptimize(c);
7269
benchmark::DoNotOptimize(x);
73-
benchmark::DoNotOptimize(y);
74-
benchmark::ClobberMemory();
70+
fill(c.begin(), c.end(), x);
71+
benchmark::DoNotOptimize(c);
72+
std::swap(x, y);
7573
}
7674
})->Range(64, 1 << 20);
7775
};

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ int main(int argc, char** argv) {
3636
Container c(size, y);
3737

3838
for ([[maybe_unused]] auto _ : st) {
39-
fill_n(c.begin(), size, x);
40-
std::swap(x, y);
4139
benchmark::DoNotOptimize(c);
4240
benchmark::DoNotOptimize(x);
43-
benchmark::DoNotOptimize(y);
44-
benchmark::ClobberMemory();
41+
fill_n(c.begin(), size, x);
42+
benchmark::DoNotOptimize(c);
43+
std::swap(x, y);
4544
}
4645
})
4746
->Arg(32)
@@ -66,12 +65,11 @@ int main(int argc, char** argv) {
6665
std::vector<bool> c(size, y);
6766

6867
for ([[maybe_unused]] auto _ : st) {
69-
fill_n(c.begin(), size, x);
70-
std::swap(x, y);
7168
benchmark::DoNotOptimize(c);
7269
benchmark::DoNotOptimize(x);
73-
benchmark::DoNotOptimize(y);
74-
benchmark::ClobberMemory();
70+
fill_n(c.begin(), size, x);
71+
benchmark::DoNotOptimize(c);
72+
std::swap(x, y);
7573
}
7674
})->Range(64, 1 << 20);
7775
};

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ int main(int argc, char** argv) {
3434
ValueType x = Generate<ValueType>::random();
3535

3636
for ([[maybe_unused]] auto _ : st) {
37-
auto f = [&x] { return x; };
38-
generate(c.begin(), c.end(), f);
3937
benchmark::DoNotOptimize(c);
40-
benchmark::DoNotOptimize(x);
41-
benchmark::ClobberMemory();
38+
generate(c.begin(), c.end(), [&x] {
39+
benchmark::DoNotOptimize(x);
40+
return x;
41+
});
42+
benchmark::DoNotOptimize(c);
4243
}
4344
})
4445
->Arg(32)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ int main(int argc, char** argv) {
3434
ValueType x = Generate<ValueType>::random();
3535

3636
for ([[maybe_unused]] auto _ : st) {
37-
auto f = [&x] { return x; };
38-
generate_n(c.begin(), size, f);
3937
benchmark::DoNotOptimize(c);
40-
benchmark::DoNotOptimize(x);
41-
benchmark::ClobberMemory();
38+
generate_n(c.begin(), size, [&x] {
39+
benchmark::DoNotOptimize(x);
40+
return x;
41+
});
42+
benchmark::DoNotOptimize(c);
4243
}
4344
})
4445
->Arg(32)

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ int main(int argc, char** argv) {
2727
{
2828
auto bm = []<class Container>(std::string name, auto move) {
2929
benchmark::RegisterBenchmark(name, [move](auto& st) {
30-
std::size_t const n = st.range(0);
31-
using ValueType = typename Container::value_type;
32-
Container c1(n), c2(n);
33-
std::generate_n(c1.begin(), n, [] { return Generate<ValueType>::random(); });
30+
std::size_t const size = st.range(0);
31+
using ValueType = typename Container::value_type;
32+
Container c1(size), c2(size);
33+
std::generate_n(c1.begin(), size, [] { return Generate<ValueType>::random(); });
3434

3535
Container* in = &c1;
3636
Container* out = &c2;
3737
for ([[maybe_unused]] auto _ : st) {
38-
benchmark::DoNotOptimize(c1);
39-
benchmark::DoNotOptimize(c2);
38+
benchmark::DoNotOptimize(in);
39+
benchmark::DoNotOptimize(out);
4040
auto result = move(in->begin(), in->end(), out->begin());
4141
benchmark::DoNotOptimize(result);
4242
std::swap(in, out);
@@ -55,24 +55,19 @@ int main(int argc, char** argv) {
5555
{
5656
auto bm = []<bool Aligned>(std::string name, auto move) {
5757
benchmark::RegisterBenchmark(name, [move](auto& st) {
58-
std::size_t const n = st.range(0);
59-
std::vector<bool> c1(n, true);
60-
std::vector<bool> c2(n, false);
58+
std::size_t const size = st.range(0);
59+
std::vector<bool> c1(size, true);
60+
std::vector<bool> c2(size, false);
6161

6262
std::vector<bool>* in = &c1;
6363
std::vector<bool>* out = &c2;
6464
for (auto _ : st) {
65-
auto first1 = in->begin();
66-
auto last1 = in->end();
67-
auto first2 = out->begin();
68-
if constexpr (Aligned) {
69-
benchmark::DoNotOptimize(move(first1, last1, first2));
70-
} else {
71-
benchmark::DoNotOptimize(move(first1 + 4, last1, first2));
72-
}
73-
std::swap(in, out);
7465
benchmark::DoNotOptimize(in);
7566
benchmark::DoNotOptimize(out);
67+
auto first = Aligned ? in->begin() : in->begin() + 4;
68+
auto result = move(first, in->end(), out->begin());
69+
benchmark::DoNotOptimize(result);
70+
std::swap(in, out);
7671
}
7772
})->Range(64, 1 << 20);
7873
};

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ int main(int argc, char** argv) {
2727
{
2828
auto bm = []<class Container>(std::string name, auto move_backward) {
2929
benchmark::RegisterBenchmark(name, [move_backward](auto& st) {
30-
std::size_t const n = st.range(0);
31-
using ValueType = typename Container::value_type;
32-
Container c1(n), c2(n);
33-
std::generate_n(c1.begin(), n, [] { return Generate<ValueType>::random(); });
30+
std::size_t const size = st.range(0);
31+
using ValueType = typename Container::value_type;
32+
Container c1(size), c2(size);
33+
std::generate_n(c1.begin(), size, [] { return Generate<ValueType>::random(); });
3434

3535
Container* in = &c1;
3636
Container* out = &c2;
3737
for ([[maybe_unused]] auto _ : st) {
38-
benchmark::DoNotOptimize(c1);
39-
benchmark::DoNotOptimize(c2);
38+
benchmark::DoNotOptimize(in);
39+
benchmark::DoNotOptimize(out);
4040
auto result = move_backward(in->begin(), in->end(), out->end());
4141
benchmark::DoNotOptimize(result);
4242
std::swap(in, out);
@@ -55,24 +55,19 @@ int main(int argc, char** argv) {
5555
{
5656
auto bm = []<bool Aligned>(std::string name, auto move_backward) {
5757
benchmark::RegisterBenchmark(name, [move_backward](auto& st) {
58-
std::size_t const n = st.range(0);
59-
std::vector<bool> c1(n, true);
60-
std::vector<bool> c2(n, false);
58+
std::size_t const size = st.range(0);
59+
std::vector<bool> c1(size, true);
60+
std::vector<bool> c2(size, false);
6161

6262
std::vector<bool>* in = &c1;
6363
std::vector<bool>* out = &c2;
6464
for (auto _ : st) {
65-
auto first1 = in->begin();
66-
auto last1 = in->end();
67-
auto last2 = out->end();
68-
if constexpr (Aligned) {
69-
benchmark::DoNotOptimize(move_backward(first1, last1, last2));
70-
} else {
71-
benchmark::DoNotOptimize(move_backward(first1, last1 - 4, last2));
72-
}
73-
std::swap(in, out);
7465
benchmark::DoNotOptimize(in);
7566
benchmark::DoNotOptimize(out);
67+
auto last = Aligned ? in->end() : in->end() - 4;
68+
auto result = move_backward(in->begin(), last, out->end());
69+
benchmark::DoNotOptimize(result);
70+
std::swap(in, out);
7671
}
7772
})->Range(64, 1 << 20);
7873
};

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ int main(int argc, char** argv) {
4646

4747
while (st.KeepRunningBatch(BatchSize)) {
4848
for (std::size_t i = 0; i != BatchSize; ++i) {
49-
auto result = remove(c[i].begin(), c[i].end(), x);
50-
benchmark::DoNotOptimize(result);
5149
benchmark::DoNotOptimize(c[i]);
5250
benchmark::DoNotOptimize(x);
53-
benchmark::ClobberMemory();
51+
auto result = remove(c[i].begin(), c[i].end(), x);
52+
benchmark::DoNotOptimize(result);
5453
}
5554

5655
st.PauseTiming();
@@ -101,11 +100,10 @@ int main(int argc, char** argv) {
101100

102101
while (st.KeepRunningBatch(BatchSize)) {
103102
for (std::size_t i = 0; i != BatchSize; ++i) {
104-
auto result = remove(c[i].begin(), c[i].end(), x);
105-
benchmark::DoNotOptimize(result);
106103
benchmark::DoNotOptimize(c[i]);
107104
benchmark::DoNotOptimize(x);
108-
benchmark::ClobberMemory();
105+
auto result = remove(c[i].begin(), c[i].end(), x);
106+
benchmark::DoNotOptimize(result);
109107
}
110108

111109
st.PauseTiming();

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ int main(int argc, char** argv) {
4242
std::vector<ValueType> out(size);
4343

4444
for ([[maybe_unused]] auto _ : st) {
45-
auto result = remove_copy(c.begin(), c.end(), out.begin(), x);
46-
benchmark::DoNotOptimize(result);
4745
benchmark::DoNotOptimize(c);
4846
benchmark::DoNotOptimize(out);
4947
benchmark::DoNotOptimize(x);
50-
benchmark::ClobberMemory();
48+
auto result = remove_copy(c.begin(), c.end(), out.begin(), x);
49+
benchmark::DoNotOptimize(result);
5150
}
5251
})
5352
->Arg(32)
@@ -81,12 +80,11 @@ int main(int argc, char** argv) {
8180
std::vector<ValueType> out(size);
8281

8382
for ([[maybe_unused]] auto _ : st) {
84-
auto result = remove_copy(c.begin(), c.end(), out.begin(), x);
85-
benchmark::DoNotOptimize(result);
8683
benchmark::DoNotOptimize(c);
8784
benchmark::DoNotOptimize(out);
8885
benchmark::DoNotOptimize(x);
89-
benchmark::ClobberMemory();
86+
auto result = remove_copy(c.begin(), c.end(), out.begin(), x);
87+
benchmark::DoNotOptimize(result);
9088
}
9189
})
9290
->Arg(32)

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,17 @@ int main(int argc, char** argv) {
3939
std::fill_n(std::back_inserter(c), size / 2, x);
4040
std::fill_n(std::back_inserter(c), size / 2, y);
4141

42-
auto pred = [&](auto& element) {
43-
benchmark::DoNotOptimize(element);
44-
return element == x;
45-
};
46-
4742
std::vector<ValueType> out(size);
4843

4944
for ([[maybe_unused]] auto _ : st) {
50-
auto result = remove_copy_if(c.begin(), c.end(), out.begin(), pred);
51-
benchmark::DoNotOptimize(result);
5245
benchmark::DoNotOptimize(c);
5346
benchmark::DoNotOptimize(out);
54-
benchmark::DoNotOptimize(x);
55-
benchmark::ClobberMemory();
47+
auto pred = [&x](auto& element) {
48+
benchmark::DoNotOptimize(element);
49+
return element == x;
50+
};
51+
auto result = remove_copy_if(c.begin(), c.end(), out.begin(), pred);
52+
benchmark::DoNotOptimize(result);
5653
}
5754
})
5855
->Arg(32)
@@ -83,20 +80,17 @@ int main(int argc, char** argv) {
8380
c.push_back(i % 2 == 0 ? x : y);
8481
}
8582

86-
auto pred = [&](auto& element) {
87-
benchmark::DoNotOptimize(element);
88-
return element == x;
89-
};
90-
9183
std::vector<ValueType> out(size);
9284

9385
for ([[maybe_unused]] auto _ : st) {
94-
auto result = remove_copy_if(c.begin(), c.end(), out.begin(), pred);
95-
benchmark::DoNotOptimize(result);
9686
benchmark::DoNotOptimize(c);
9787
benchmark::DoNotOptimize(out);
98-
benchmark::DoNotOptimize(x);
99-
benchmark::ClobberMemory();
88+
auto pred = [&](auto& element) {
89+
benchmark::DoNotOptimize(element);
90+
return element == x;
91+
};
92+
auto result = remove_copy_if(c.begin(), c.end(), out.begin(), pred);
93+
benchmark::DoNotOptimize(result);
10094
}
10195
})
10296
->Arg(32)

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ int main(int argc, char** argv) {
5151

5252
while (st.KeepRunningBatch(BatchSize)) {
5353
for (std::size_t i = 0; i != BatchSize; ++i) {
54+
benchmark::DoNotOptimize(c[i]);
5455
auto result = remove_if(c[i].begin(), c[i].end(), pred);
5556
benchmark::DoNotOptimize(result);
56-
benchmark::DoNotOptimize(c[i]);
57-
benchmark::DoNotOptimize(x);
58-
benchmark::ClobberMemory();
5957
}
6058

6159
st.PauseTiming();
@@ -111,11 +109,9 @@ int main(int argc, char** argv) {
111109

112110
while (st.KeepRunningBatch(BatchSize)) {
113111
for (std::size_t i = 0; i != BatchSize; ++i) {
112+
benchmark::DoNotOptimize(c[i]);
114113
auto result = remove_if(c[i].begin(), c[i].end(), pred);
115114
benchmark::DoNotOptimize(result);
116-
benchmark::DoNotOptimize(c[i]);
117-
benchmark::DoNotOptimize(x);
118-
benchmark::ClobberMemory();
119115
}
120116

121117
st.PauseTiming();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ int main(int argc, char** argv) {
4242
std::fill_n(std::back_inserter(c), size / 2, y);
4343

4444
for ([[maybe_unused]] auto _ : st) {
45-
replace(c.begin(), c.end(), x, z);
46-
std::swap(x, z);
4745
benchmark::DoNotOptimize(c);
4846
benchmark::DoNotOptimize(x);
4947
benchmark::DoNotOptimize(z);
50-
benchmark::ClobberMemory();
48+
replace(c.begin(), c.end(), x, z);
49+
benchmark::DoNotOptimize(c);
50+
std::swap(x, z);
5151
}
5252
})
5353
->Arg(32)
@@ -79,12 +79,12 @@ int main(int argc, char** argv) {
7979
}
8080

8181
for ([[maybe_unused]] auto _ : st) {
82-
replace(c.begin(), c.end(), x, z);
83-
std::swap(x, z);
8482
benchmark::DoNotOptimize(c);
8583
benchmark::DoNotOptimize(x);
8684
benchmark::DoNotOptimize(z);
87-
benchmark::ClobberMemory();
85+
replace(c.begin(), c.end(), x, z);
86+
benchmark::DoNotOptimize(c);
87+
std::swap(x, z);
8888
}
8989
})
9090
->Arg(32)

0 commit comments

Comments
 (0)