]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Limit allocation on iterator insertion in Hashtable [PR 96088]
authorFrançois Dumont <fdumont@gcc.gnu.org>
Tue, 25 Aug 2020 19:31:23 +0000 (21:31 +0200)
committerFrançois Dumont <fdumont@gcc.gnu.org>
Mon, 24 May 2021 19:51:06 +0000 (21:51 +0200)
commit2c43f5ec9db163696de8691eb529df06c4999bcc
tree43b6f57544634afafbdca1e491e1b58f4c6bd89a
parenta8764071f2eb6b4cdc9ecb788dfaa2b095b52598
libstdc++: Limit allocation on iterator insertion in Hashtable [PR 96088]

When inserting into unordered_multiset or unordered_multimap first instantiate
the node to store and compute the hash code from it to avoid a potential
intermediate key_type instantiation.

When inserting into unordered_set or unordered_map check if invoking the hash
functor with container key_type is noexcept and invoking the same hash functor
with key part of the iterator value_type can throw. In this case create a
temporary key_type instance at Hashtable level and use it to compute the hash
code. This temporary instance is moved to the final storage location if needed.

libstdc++-v3/ChangeLog:

PR libstdc++/96088
* include/bits/hashtable_policy.h (_Select2nd): New.
(_NodeBuilder<>): New.
(_ReuseOrAllocNode<>::operator()): Use variadic template args.
(_AllocNode<>::operator()): Likewise.
* include/bits/hashtable.h
(_Hashtable<>::__node_builder_t): New.
(_Hashtable<>::_M_insert_unique<>(_Kt&&, _Arg&&, const _NodeGenerator&)):
 New.
(_Hashtable<>::_S_forward_key): New.
(_Hashtable<>::_M_insert): Use latter.
(_Hashtable<>::_M_insert(const_iterator, _Arg&&, const _NodeGenerator&, false_type)):
Instantiate node first, compute hash code second.
* testsuite/23_containers/unordered_map/96088.cc: New test.
* testsuite/23_containers/unordered_multimap/96088.cc: New test.
* testsuite/23_containers/unordered_multiset/96088.cc: New test.
* testsuite/23_containers/unordered_set/96088.cc: New test.
* testsuite/util/replacement_memory_operators.h
(counter::_M_increment): New.
(counter::_M_decrement): New.
(counter::reset()): New.
libstdc++-v3/include/bits/hashtable.h
libstdc++-v3/include/bits/hashtable_policy.h
libstdc++-v3/testsuite/23_containers/unordered_map/96088.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/unordered_multimap/96088.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/unordered_multiset/96088.cc [new file with mode: 0644]
libstdc++-v3/testsuite/23_containers/unordered_set/96088.cc [new file with mode: 0644]
libstdc++-v3/testsuite/util/replacement_memory_operators.h