Skip to content

Commit 5dcd917

Browse files
<functional>: make move_only_function do less work (#5328)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 48db4fa commit 5dcd917

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ add_benchmark(iota src/iota.cpp)
117117
add_benchmark(locale_classic src/locale_classic.cpp)
118118
add_benchmark(minmax_element src/minmax_element.cpp)
119119
add_benchmark(mismatch src/mismatch.cpp)
120+
add_benchmark(move_only_function src/move_only_function.cpp)
120121
add_benchmark(path_lexically_normal src/path_lexically_normal.cpp)
121122
add_benchmark(priority_queue_push_range src/priority_queue_push_range.cpp)
122123
add_benchmark(random_integer_generation src/random_integer_generation.cpp)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <benchmark/benchmark.h>
5+
#include <functional>
6+
#include <utility>
7+
8+
using namespace std;
9+
10+
void mof_none(benchmark::State& state) {
11+
move_only_function<void()> mof;
12+
for (auto _ : state) {
13+
benchmark::DoNotOptimize(mof);
14+
}
15+
}
16+
17+
void mof_construct(benchmark::State& state) {
18+
for (auto _ : state) {
19+
move_only_function<void()> mof;
20+
benchmark::DoNotOptimize(mof);
21+
}
22+
}
23+
24+
void mof_move(benchmark::State& state) {
25+
move_only_function<void()> mof;
26+
for (auto _ : state) {
27+
benchmark::DoNotOptimize(mof);
28+
auto moved_mof = move(mof);
29+
benchmark::DoNotOptimize(moved_mof);
30+
}
31+
}
32+
33+
void mof_construct_and_move(benchmark::State& state) {
34+
for (auto _ : state) {
35+
move_only_function<void()> mof;
36+
benchmark::DoNotOptimize(mof);
37+
auto moved_mof = move(mof);
38+
benchmark::DoNotOptimize(moved_mof);
39+
}
40+
}
41+
42+
BENCHMARK(mof_none);
43+
BENCHMARK(mof_construct);
44+
BENCHMARK(mof_move);
45+
BENCHMARK(mof_construct_and_move);
46+
47+
BENCHMARK_MAIN();

stl/inc/functional

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,11 +1506,6 @@ public:
15061506
_Other._Reset_to_null();
15071507
}
15081508

1509-
void _Construct_with_null() noexcept {
1510-
_Data._Impl = nullptr;
1511-
_Data._Set_large_fn_ptr(nullptr); // initialize, since we'll be copying it
1512-
}
1513-
15141509
void _Reset_to_null() noexcept {
15151510
_Data._Impl = nullptr;
15161511
}
@@ -1888,11 +1883,11 @@ public:
18881883
using typename _Call::result_type;
18891884

18901885
move_only_function() noexcept {
1891-
this->_Construct_with_null();
1886+
this->_Reset_to_null();
18921887
}
18931888

18941889
move_only_function(nullptr_t) noexcept {
1895-
this->_Construct_with_null();
1890+
this->_Reset_to_null();
18961891
}
18971892

18981893
move_only_function(move_only_function&&) noexcept = default;
@@ -1906,7 +1901,7 @@ public:
19061901

19071902
if constexpr (is_member_pointer_v<_Vt> || is_pointer_v<_Vt> || _Is_specialization_v<_Vt, move_only_function>) {
19081903
if (_Callable == nullptr) {
1909-
this->_Construct_with_null();
1904+
this->_Reset_to_null();
19101905
return;
19111906
}
19121907
}

0 commit comments

Comments
 (0)