Skip to content
Open
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>);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benchmark is very simple, but I think it's enough to show the compiler cannot optimize away the copy.


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

void swap(any& _That) noexcept {
_That = _STD exchange(*this, _STD move(_That));
any _Old = _STD move(*this);
_Assign(_STD move(_That));
Copy link
Contributor Author

@achabense achabense Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are still paying an additional copy for some insane self-swaps here...

std::any a = std::make_any<std::any>(42);
std::any& a_o = *std::any_cast<std::any>(&a);
a.swap(a_o);
a_o.swap(a);

(Also, I think _Assign(any-prvalue) is somewhat misleading (for example #5413)), and it's better to create the temporal any explicitly in each function. For example, this would be v.)

any _Old_this = _STD move(*this);
{
    any _Old_that = _STD move(_That);
    reset();
    _Move_from(_Old_that);
}
_That.reset();
_That._Move_from(_Old_this);

_That.reset();
_That._Move_from(_Old);
}

// Observers [any.observers]
Expand Down