]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Allow unordered_set assignment to assign to existing nodes
authorJonathan Wakely <jwakely@redhat.com>
Fri, 1 Nov 2024 12:49:53 +0000 (12:49 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 13 Nov 2024 20:21:39 +0000 (20:21 +0000)
commitafc9351ebbac1ed55d7c472af817b172bdb662e6
tree6797dd7a24eb84b8da3a9b351313b9803bd83b36
parent9fcbbb3d104717447cffb65f6ef000969a7b7bb4
libstdc++: Allow unordered_set assignment to assign to existing nodes

Currently the _ReuseOrAllocNode::operator(Args&&...) function always
destroys the value stored in recycled nodes and constructs a new value.

The _ReuseOrAllocNode type is only ever used for implementing
assignment, either from another unordered container of the same type, or
from std::initializer_list<value_type>. Consequently, the parameter pack
Args only ever consists of a single parameter or type const value_type&
or value_type.  We can replace the variadic parameter pack with a single
forwarding reference parameter, and when the value_type is assignable
from that type we can use assignment instead of destroying the existing
value and then constructing a new one.

Using assignment is typically only possible for sets, because for maps
the value_type is std::pair<const key_type, mapped_type> and in most
cases std::is_assignable_v<const key_type&, const key_type&> is false.

libstdc++-v3/ChangeLog:

* include/bits/hashtable_policy.h (_ReuseOrAllocNode::operator()):
Replace parameter pack with a single parameter. Assign to
existing value when possible.
* testsuite/23_containers/unordered_multiset/allocator/move_assign.cc:
Adjust expected count of operations.
* testsuite/23_containers/unordered_set/allocator/move_assign.cc:
Likewise.

Reviewed-by: François Dumont <fdumont@gcc.gnu.org>
libstdc++-v3/include/bits/hashtable_policy.h
libstdc++-v3/testsuite/23_containers/unordered_multiset/allocator/move_assign.cc
libstdc++-v3/testsuite/23_containers/unordered_set/allocator/move_assign.cc