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 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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 List-Id: 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; } }; -- 2.41.0