public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-8132] libstdc++: Micro-optimization for std::optional [PR112480]
@ 2023-12-06 14:44 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-12-06 14:44 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:866870c51d58881819db6db76dcdfe3f43d89903

commit r13-8132-g866870c51d58881819db6db76dcdfe3f43d89903
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sat Nov 11 00:35:18 2023 +0000

    libstdc++: Micro-optimization for std::optional [PR112480]
    
    This small change removes a branch when clearing a std::optional<T> 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.
    
    (cherry picked from commit 2c492f99fc1fcb5f598286c3f3a21a05bca69d9e)

Diff:
---
 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 27260f9cc38..29f534b9861 100644
--- a/libstdc++-v3/include/std/optional
+++ b/libstdc++-v3/include/std/optional
@@ -316,6 +316,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;
       }
     };

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-12-06 14:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-06 14:44 [gcc r13-8132] libstdc++: Micro-optimization for std::optional [PR112480] Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).