[Bug libstdc++/112480] optional<T>::reset emits inefficient code when T is trivially-destructible

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Nov 13 12:22:36 GMT 2023


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112480

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Good point, it looks like we get the same codegen improvement for ~T(){} even
at -O1 if we don't restrict it to trivially destructible types.

There seems to be no difference in codegen for _M_engaged=false or
_M_engaged=_M_engaged (which isn't too surprising, since we know that either it
was already false, or we're setting it to false). Given that, I think I prefer
explicitly setting to false.

So I'll test this:

--- 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;
       }
     };


More information about the Gcc-bugs mailing list