From patchwork Mon Nov 13 23:22:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 164689 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp1561709vqg; Mon, 13 Nov 2023 16:26:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IH64XMFSmOA/9kL0Gu1v36TCjx1R4j2+eIgklxg77cSSZ1FGhU74+b+xOlP5AJAaUi8qbM4 X-Received: by 2002:ac8:58cf:0:b0:41c:f729:8032 with SMTP id u15-20020ac858cf000000b0041cf7298032mr865643qta.63.1699921616967; Mon, 13 Nov 2023 16:26:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699921616; cv=pass; d=google.com; s=arc-20160816; b=zw1vtyQGmCMaOjpGuTGSpNkzGmTgdBfzW40MYdciBIm3J3lq7lw6M5TKeLlbahRiXl 3M945h8e+MhlSEjqCR+XusbyyRhe85HfgJkFyAJ3/S2GYCMSX+EW/DC5Noby5Rz8d/F/ t3oTqn9bCbpq2MDkEEj0IuzrOpHwce2fFBTnATR40Ap1PFT7zL760xLfZ1PyebyNFQyn W4Pb13AYOZ4zc4jb9num4uHIpJUkGwpBVrz9QnoQ+NsEAVtggtUyeJ1yc4neJ7yTffDa XsWuN3QBh/euWPr3KXCirOCCjzM5ZfM/ohP3guB4dEj3FQOxxPkTorto+gyCH7JQhh5K NiyA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=jMSQ2qEBeirkynhmnKE5fMQ7ug4yoy8J2/HWEtV/gWg=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=b1/+fPoVBGkQlYh2l074FtKQ/1YBmKPoAvWhksczJD9SJ9RVjZ1vKeW9SuOSRt6Dk1 r6nqMh1/wxcwer4ZB5jcI33OQt0R4Q+HoRfQ2IMYrQvie2GtCLngiaMeChBmsoGrfK8s GzQ7WrJMjzQJdlOpuUB74/fP1SvgX+xdrhMigaJ7rhB6X4btVKqrdPNnF16N5RacgOoT AVCyn5SN9cL5nKPf0X0MuckjZFnALc6lSqlw1X9H2NYT5zPkBGbhIcBaozGAH/uhkr8y HVsEiGAXjJM7RN733HzCeM4bcSyMDeQgwg5cDey7r9h741VcH+l0wLpyNdwzN0vmiyA/ MuVQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J64k4ILz; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w3-20020ac857c3000000b003f4ecb4085bsi5525094qta.289.2023.11.13.16.26.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 16:26:56 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=J64k4ILz; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 97C0F393E25C for ; Mon, 13 Nov 2023 23:23:42 +0000 (GMT) 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 D2B153853306 for ; Mon, 13 Nov 2023 23:23:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D2B153853306 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D2B153853306 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699917798; cv=none; b=PkibM8nt6CdBB9jvkJV2vp+tUGLwuAs/F6qoeu0NA2H0dcGPVByieuJq/dwJeNszNzgzB+eVw+9fGz2tXpZGtXOYjKBwThLX4tCMJWr2aa7yoLyOgMheWEVNUHdBZnC9sFykdcG3Ikylr4Pzt0PjsDTKR6p3RzgdpUs/Z7dCwL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699917798; c=relaxed/simple; bh=JtyaS2jzGsKfsfSVEryKCP5p8OexW2jgyC0syWSTMAs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hEVBettAhxrAtA6BjLtI+5CWQS7r+lzBhCSkA7Au1aGxBsEfzcWTiP7VlXqkhp2p/j/lhHxYIOTmSaD22hAq4GnX4P702tYq7jwFf7t0QKngCoqVFC2HT0ZIfgoCLDPEyGUcwGMWTfUO/qurgcfnP2kyWckdXnP/vZKl+fhDXHI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699917796; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jMSQ2qEBeirkynhmnKE5fMQ7ug4yoy8J2/HWEtV/gWg=; b=J64k4ILzPPU5C/fiIBzii/7zpPFKSSxtAVePrObMhFXQMVvTpJ0XBOouD6Hl7orBvVmWNf lmyGW4QHhlzrL8Wq3TGM7Ge7gtkjc95s4oS8yAWp6sT8rL2KiNyENx9HEhhjl4Dyev5ogg TZ8Yst9CWVjBcSRpS/R4wX7GWIkhn1k= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-149-ou4ajdqRM56zZivBHhcYbQ-1; Mon, 13 Nov 2023 18:23:14 -0500 X-MC-Unique: ou4ajdqRM56zZivBHhcYbQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 675993806631; Mon, 13 Nov 2023 23:23:14 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 308141121307; Mon, 13 Nov 2023 23:23:14 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Micro-optimization for std::optional [PR112480] Date: Mon, 13 Nov 2023 23:22:54 +0000 Message-ID: <20231113232313.809520-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782497009683849156 X-GMAIL-MSGID: 1782497009683849156 Tested x86_64-linux. Pushed to trunk. -- >8 -- This small change removes a branch when clearing a std::optional for types with no-op destructors. For types where the destructor can be optimized away (e.g. because it's trivial, or empty and can be inlined) the _M_destroy() function does nothing but set _M_engaged to false. Setting _M_engaged=false unconditionally is cheaper than only doing it when initially true, because it allows the compiler to remove a branch. The compiler thinks it would be incorrect to unconditionally introduce a store there, because it could conflict with reads in other threads, so it won't do that optimization itself. We know it's safe to do because we're in a non-const member function, so the standard forbids any potentially concurrent calls to other member functions of the same object. Making the store unconditional can't create a data race that isn't already present in the program. libstdc++-v3/ChangeLog: PR libstdc++/112480 * include/std/optional (_Optional_payload_base::_M_reset): Set _M_engaged to false unconditionally. --- libstdc++-v3/include/std/optional | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 53450c760d9..a8c97717b72 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -311,6 +311,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (this->_M_engaged) _M_destroy(); + else // This seems redundant but improves codegen, see PR 112480. + this->_M_engaged = false; } };