-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Optimize any::swap
#5710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Optimize any::swap
#5710
Conversation
c3497f0
to
5567c9c
Compare
@@ -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)); |
There was a problem hiding this comment.
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);
|
||
BENCHMARK(bm<trivial>); | ||
BENCHMARK(bm<small>); | ||
BENCHMARK(bm<large>); |
There was a problem hiding this comment.
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.
This pr removes an unnecessary copy in
any::swap
.Hopefully fixes #5698.
Benchmark result: