From patchwork Wed Aug 3 12:31:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 373 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp313799pxt; Wed, 3 Aug 2022 05:32:58 -0700 (PDT) X-Google-Smtp-Source: AGRyM1temBLZNnDHqzh93TVPSsXZVtzMq+myogbfyE9V2XzyDDZ+7HYyrPSp5Y21pXEL93c5OTx5 X-Received: by 2002:aa7:cad3:0:b0:43c:e615:13e2 with SMTP id l19-20020aa7cad3000000b0043ce61513e2mr25032692edt.289.1659529978307; Wed, 03 Aug 2022 05:32:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659529978; cv=none; d=google.com; s=arc-20160816; b=AKiWoU10Fsi2a2zKR0dQX+jK53CN7dOdnpaha9zYY4Nl2yBvZg7WTJKS+KMbw7mTYN p4kzeoA+jGECepX3GLZMTyg69g3T/TFBK/tWfXwARArjdNdtRI7CKCNKKwVQrbnar9Wc KY+9BmLVqYNDzOdSkSCLIAX1c7zeZmZ6wsFxfMoDcg0bqSPrNZzDww+os8g2j3dZ1cst jT58bIzLDIq3JVQQJkH0r3nH+kePFLXvuxTHZRUSlDoz5qDGMLnKLdL/OPYi1qYkNfsu +csUzXliXi3CmBHDXzAzoCFZIsSzrT8RNW9pLh14aCZqvKxYgxJ7YtXFuWPAYq6OTLM4 IHhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=J3jsGACcibOL9A7AbAp4bDAyoPi9Dnb/syQDljXZrkM=; b=02nBztttUZ3kOJ+Rsk0rHnnsqNfCadewRsokHjKOiJndywreK7L3aXAIkfAyAVt4lY gKQ5GhEqT0umrCZKjmebz3yG1XQjdWHyBmKyIrInORtIIFOzlWoqM7qLBUX0U/yw5OXa 1TUrpvgzq+9N60rfTAiUHmm1aknYJJMCZtm1NymWi2aDRanzi+BztuUXCRxcvLXf287w Bx3jFUT/ydVhqv01+sVDcMZFUqrEouODf20z4eC3ZPoFmrIBuvPeHlc7aD1zSBImhQat 1qMBmtDFcBwqdI45gDjD4xsbT1UPXGpZnYYZyScLuP6BEbRppDxrg3ObkV/RHILkPEzE NKIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=DBACwGHM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x12-20020a05640226cc00b0043d5efd8075si4634936edd.41.2022.08.03.05.32.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Aug 2022 05:32:58 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=DBACwGHM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D7088385840A for ; Wed, 3 Aug 2022 12:32:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D7088385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1659529942; bh=J3jsGACcibOL9A7AbAp4bDAyoPi9Dnb/syQDljXZrkM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=DBACwGHMKRStqPb/QpkB0FojIp2IPrAshHvapIk4RAVg1Iw3GC+9hLu1NzjP5Wj7w iNw9UxQvKp3J3CWxFV2KYKe48YHVT2ZUq/gFUnHyaUt3wy/Y6QWNI23yzEFtZoCe5B YKvkOXaNd+KG1Ca8/rR1TWoTdrkyWXr9CqO+198g= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 8C8703883032 for ; Wed, 3 Aug 2022 12:31:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8C8703883032 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) 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 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1740143306483014433?= X-GMAIL-MSGID: =?utf-8?q?1740143306483014433?= 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 } }