From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 3C14C3885534 for ; Wed, 3 Aug 2022 12:31:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3C14C3885534 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-561-naOr2LXvNEivk0enwSsyjg-1; Wed, 03 Aug 2022 08:31:37 -0400 X-MC-Unique: naOr2LXvNEivk0enwSsyjg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 511AB101A589; Wed, 3 Aug 2022 12:31:37 +0000 (UTC) Received: from localhost (unknown [10.33.36.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 16D4F18EA8; Wed, 3 Aug 2022 12:31:36 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Avoid try-catch and O(N) size in std::list::merge for old ABI Date: Wed, 3 Aug 2022 13:31:36 +0100 Message-Id: <20220803123136.589621-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Aug 2022 12:31:42 -0000 Tested x86_64-linux, pushed to gcc-10 branch. -- >8 -- 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. --- 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 } } -- 2.37.1