From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 612C83858022; Thu, 9 Dec 2021 23:26:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 612C83858022 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-5876] libstdc++: Avoid unnecessary allocations in std::map insertions [PR92300] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: fb9875ebf10c86be21824cb836b7b3b80f3a731b X-Git-Newrev: db5fa0837e464b595a3d63766060bae1c9ac5ccc Message-Id: <20211209232649.612C83858022@sourceware.org> Date: Thu, 9 Dec 2021 23:26:49 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Dec 2021 23:26:49 -0000 https://gcc.gnu.org/g:db5fa0837e464b595a3d63766060bae1c9ac5ccc commit r12-5876-gdb5fa0837e464b595a3d63766060bae1c9ac5ccc Author: Jonathan Wakely Date: Fri Dec 3 14:33:13 2021 +0000 libstdc++: Avoid unnecessary allocations in std::map insertions [PR92300] Inserting a pair into a map will allocate a new node and construct a pair in the node, then check if the Key is already present in the map. That is because pair is not the same type as the map's value_type. But it only differs in the const-qualification on the Key, and so we should be able to do the lookup directly, without allocating a new node. This avoids allocating and then deallocating a node for the case where the key is already found and nothing gets inserted. We can take this optimization further and lookup the key directly for a pair, pair, pair etc. for any X. A strict reading of the standard says we can only do this when we know the allocator won't do anything funky with the value when constructing a pair from a slightly different type. Inserting that type only requires the value_type to be Cpp17EmplaceInsertable into the container, and that doesn't have any requirement that the value is unchanged (unlike Cpp17CopyInsertable and Cpp17MoveInsertable). For that reason, the optimization is only done for maps using std::allocator. A similar optimization can be done for map.emplace(key, value) where the first argument is similar to the key_type and so can be looked up without allocating a new node and constructing a key_type. Finally, both of the insert and emplace cases can use the same optimization when key_type is a scalar type and some other scalar is being passed as the insert/emplace argument. Converting from one scalar type to another won't have surprising value-altering behaviour, and has no side effects (unlike e.g. constructing a std::string from a const char* argument, which might allocate). We don't need to do this for std::multimap, because we always insert the new node even if the key is already present. So there's no benefit to doing the lookup before allocating the new node. libstdc++-v3/ChangeLog: PR libstdc++/92300 * include/bits/stl_map.h (insert(Pair&&), emplace(Args&&...)): Check whether the arguments can be looked up directly without constructing a temporary node first. * include/bits/stl_pair.h (__is_pair): Move to here, from ... * include/bits/uses_allocator_args.h (__is_pair): ... here. * testsuite/23_containers/map/modifiers/emplace/92300.cc: New test. * testsuite/23_containers/map/modifiers/insert/92300.cc: New test. Diff: --- libstdc++-v3/include/bits/stl_map.h | 49 +++++++++++++++++++++- libstdc++-v3/include/bits/stl_pair.h | 9 ++++ libstdc++-v3/include/bits/uses_allocator_args.h | 6 --- .../23_containers/map/modifiers/emplace/92300.cc | 36 ++++++++++++++++ .../23_containers/map/modifiers/insert/92300.cc | 38 +++++++++++++++++ 5 files changed, 130 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h index cc87f11fb11..658d5865138 100644 --- a/libstdc++-v3/include/bits/stl_map.h +++ b/libstdc++-v3/include/bits/stl_map.h @@ -154,6 +154,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits; +#if __cplusplus >= 201703L + template> + static constexpr bool __usable_key + = __or_v, + __and_, is_scalar<_Key>>>; +#endif + public: // many of these are specified differently in ISO, but the following are // "functionally equivalent" @@ -574,7 +581,27 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER template std::pair emplace(_Args&&... __args) - { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } + { +#if __cplusplus >= 201703L + if constexpr (sizeof...(_Args) == 2) + if constexpr (is_same_v>) + { + auto&& [__a, __v] = pair<_Args&...>(__args...); + if constexpr (__usable_key) + { + const key_type& __k = __a; + iterator __i = lower_bound(__k); + if (__i == end() || key_comp()(__k, (*__i).first)) + { + __i = emplace_hint(__i, std::forward<_Args>(__args)...); + return {__i, true}; + } + return {__i, false}; + } + } +#endif + return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); + } /** * @brief Attempts to build and insert a std::pair into the %map. @@ -814,7 +841,25 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER __enable_if_t::value, pair> insert(_Pair&& __x) - { return _M_t._M_emplace_unique(std::forward<_Pair>(__x)); } + { +#if __cplusplus >= 201703L + using _P2 = remove_reference_t<_Pair>; + if constexpr (__is_pair<_P2>) + if constexpr (is_same_v>) + if constexpr (__usable_key) + { + const key_type& __k = __x.first; + iterator __i = lower_bound(__k); + if (__i == end() || key_comp()(__k, (*__i).first)) + { + __i = emplace_hint(__i, std::forward<_Pair>(__x)); + return {__i, true}; + } + return {__i, false}; + } +#endif + return _M_t._M_emplace_unique(std::forward<_Pair>(__x)); + } #endif /// @} diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index 6081e0c7fe9..5f7b60932e4 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -777,6 +777,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline constexpr size_t tuple_size_v> = 2; + + template + inline constexpr bool __is_pair = false; + + template + inline constexpr bool __is_pair> = true; + + template + inline constexpr bool __is_pair> = true; #endif /// @cond undocumented diff --git a/libstdc++-v3/include/bits/uses_allocator_args.h b/libstdc++-v3/include/bits/uses_allocator_args.h index 8b548ff6981..e1fd7e7d611 100644 --- a/libstdc++-v3/include/bits/uses_allocator_args.h +++ b/libstdc++-v3/include/bits/uses_allocator_args.h @@ -56,12 +56,6 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION - template - inline constexpr bool __is_pair = false; - template - inline constexpr bool __is_pair> = true; - template - inline constexpr bool __is_pair> = true; template concept _Std_pair = __is_pair<_Tp>; diff --git a/libstdc++-v3/testsuite/23_containers/map/modifiers/emplace/92300.cc b/libstdc++-v3/testsuite/23_containers/map/modifiers/emplace/92300.cc new file mode 100644 index 00000000000..937b4d9a103 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/modifiers/emplace/92300.cc @@ -0,0 +1,36 @@ +// { dg-do run { target c++17 } } + +#include +#include + +bool oom = false; + +void* operator new(std::size_t n) +{ + if (oom) + throw std::bad_alloc(); + return std::malloc(n); +} + +void operator delete(void* p) +{ + std::free(p); +} + +void operator delete(void* p, std::size_t) +{ + std::free(p); +} + +int main() +{ + std::map m; + int i = 0; + (void) m[i]; + oom = true; + m.emplace(i, 1); + m.emplace(i, 2L); + const int c = 3; + m.emplace(i, c); + m.emplace((long)i, 4); +} diff --git a/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/92300.cc b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/92300.cc new file mode 100644 index 00000000000..80abdaf1f30 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/modifiers/insert/92300.cc @@ -0,0 +1,38 @@ +// { dg-do run { target c++17 } } + +#include +#include + +bool oom = false; + +void* operator new(std::size_t n) +{ + if (oom) + throw std::bad_alloc(); + return std::malloc(n); +} + +void operator delete(void* p) +{ + std::free(p); +} + +void operator delete(void* p, std::size_t) +{ + std::free(p); +} + +int main() +{ + using std::pair; + std::map m; + int i = 0; + (void) m[i]; + oom = true; + m.insert({i, 1}); // insert(value_type&&) + m.insert(pair(i, 2)); // insert(Pair&&) + m.insert(pair(i, 3)); // insert(Pair&&) + m.insert(pair(i, 4L)); // insert(Pair&&) + m.insert(pair(i, 5L)); // insert(Pair&&) + m.insert(pair(i, 6L)); // insert(Pair&&) +}