Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ endfunction()

add_benchmark(adjacent_difference src/adjacent_difference.cpp)
add_benchmark(adjacent_find src/adjacent_find.cpp)
add_benchmark(any_swap src/any_swap.cpp)
add_benchmark(bitset_from_string src/bitset_from_string.cpp)
add_benchmark(bitset_to_string src/bitset_to_string.cpp)
add_benchmark(efficient_nonlocking_print src/efficient_nonlocking_print.cpp)
Expand Down
42 changes: 42 additions & 0 deletions benchmarks/src/any_swap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <any>
#include <array>
#include <benchmark/benchmark.h>
#include <type_traits>

using trivial = std::array<int, 2>;
static_assert(std::is_trivially_copyable_v<trivial>);

struct small {
std::array<int, 4> c{};
small() = default;
small(const small&) = default;
small& operator=(const small&) = default;
small(small&&) noexcept = default;
small& operator=(small&&) noexcept = default;
~small() {}
};
static_assert(!std::is_trivially_copyable_v<small>);
static_assert(std::is_nothrow_move_constructible_v<small>);

using large = std::array<int, 32>;

template <class T>
void bm(benchmark::State& state) {
std::any a = T{};
std::any b = T{};

for (auto _ : state) {
a.swap(b);
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
}
}

BENCHMARK(bm<trivial>);
BENCHMARK(bm<small>);
BENCHMARK(bm<large>);
Comment thread
StephanTLavavej marked this conversation as resolved.

BENCHMARK_MAIN();
6 changes: 5 additions & 1 deletion stl/inc/any
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ public:
}

void swap(any& _That) noexcept {
_That = _STD exchange(*this, _STD move(_That));
any _Old = _STD move(*this);
reset();
_Move_from(_That);
_That.reset();
_That._Move_from(_Old);
}

// Observers [any.observers]
Expand Down