From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 765AD3885530; Wed, 3 Aug 2022 12:30:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 765AD3885530 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 r10-10931] libstdc++: Avoid try-catch and O(N) size in std::list::merge for old ABI X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: d1b51c15312940779b1288b59973d88b9592dd23 X-Git-Newrev: f05b125a8d788a84dc7361b52f54af6169ad8b32 Message-Id: <20220803123052.765AD3885530@sourceware.org> Date: Wed, 3 Aug 2022 12:30:52 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Aug 2022 12:30:52 -0000 https://gcc.gnu.org/g:f05b125a8d788a84dc7361b52f54af6169ad8b32 commit r10-10931-gf05b125a8d788a84dc7361b52f54af6169ad8b32 Author: Jonathan Wakely Date: Wed Sep 29 20:46:55 2021 +0100 libstdc++: Avoid try-catch and O(N) size in std::list::merge for old ABI The current std::list::merge code calls size() before starting to merge any elements, so that the _M_size members can be updated after the merge finishes. The work is done in a try-block so that the sizes can still be updated in an exception handler if any element comparison throws. The _M_size members only exist for the cxx11 ABI, so the initial call to size() and the try-catch are only needed for that ABI. For the old ABI the size() call performs an O(N) list traversal to get a value that isn't even used, and catching exceptions just to rethrow them isn't needed either. In r11-10123 this code was refactored to use an RAII guard type, but for the gcc-10 branch a less invasive change using preprocessor conditionals seems more appropriate. libstdc++-v3/ChangeLog: * include/bits/list.tcc (list::merge) [!USE_CXX11_ABI]: Remove call to size() and try-catch block. Diff: --- libstdc++-v3/include/bits/list.tcc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libstdc++-v3/include/bits/list.tcc b/libstdc++-v3/include/bits/list.tcc index ce9e983c539..1f27d8b786a 100644 --- a/libstdc++-v3/include/bits/list.tcc +++ b/libstdc++-v3/include/bits/list.tcc @@ -406,8 +406,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER iterator __last1 = end(); iterator __first2 = __x.begin(); iterator __last2 = __x.end(); +#if _GLIBCXX_USE_CXX11_ABI const size_t __orig_size = __x.size(); __try { +#endif while (__first1 != __last1 && __first2 != __last2) if (*__first2 < *__first1) { @@ -422,6 +424,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER this->_M_inc_size(__x._M_get_size()); __x._M_set_size(0); +#if _GLIBCXX_USE_CXX11_ABI } __catch(...) { @@ -430,6 +433,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER __x._M_set_size(__dist); __throw_exception_again; } +#endif } }