public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/101406] New: shared_ptr in _S_atomic mode still uses __atomic_add_dispatch()
@ 2021-07-10 20:51 marc.mutz at kdab dot com
  2021-07-11  8:52 ` [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation marc.mutz at kdab dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: marc.mutz at kdab dot com @ 2021-07-10 20:51 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101406
           Summary: shared_ptr in _S_atomic mode still uses
                    __atomic_add_dispatch()
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marc.mutz at kdab dot com
  Target Milestone: ---

Consider

     // https://godbolt.org/z/efTW6MoEh
     void test_copy(const std::shared_ptr<int> &sp)
     { auto copy = sp; }

vs.

     // https://godbolt.org/z/3aoGq1f9P
     void test_copy(const boost::shared_ptr<int> &sp)
     { auto copy = sp; }

In the first cast, over 70 lines of assembler are emitted, in the second,
around 30. This seems to be in large part because in
_Sp_counted_base::_M_add_ref_copy(), you're using __atomic_add_dispatch() even
if _Lp is _S_atomic. It seems to me that a specialisation of this function
template for _S_atomic calling just __atomic_add() is missing:
https://godbolt.org/z/crPz9hGe7

Probably same for the deref case, too.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation
  2021-07-10 20:51 [Bug libstdc++/101406] New: shared_ptr in _S_atomic mode still uses __atomic_add_dispatch() marc.mutz at kdab dot com
@ 2021-07-11  8:52 ` marc.mutz at kdab dot com
  2021-07-12 10:44 ` redi at gcc dot gnu.org
  2021-07-12 14:21 ` marc.mutz at kdab dot com
  2 siblings, 0 replies; 4+ messages in thread
From: marc.mutz at kdab dot com @ 2021-07-11  8:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Marc Mutz <marc.mutz at kdab dot com> ---
Comparison to the other two major standard library implementations:
https://godbolt.org/z/crPo44rxo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation
  2021-07-10 20:51 [Bug libstdc++/101406] New: shared_ptr in _S_atomic mode still uses __atomic_add_dispatch() marc.mutz at kdab dot com
  2021-07-11  8:52 ` [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation marc.mutz at kdab dot com
@ 2021-07-12 10:44 ` redi at gcc dot gnu.org
  2021-07-12 14:21 ` marc.mutz at kdab dot com
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2021-07-12 10:44 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |WONTFIX

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Marc Mutz from comment #0)
> Consider
> 
>      // https://godbolt.org/z/efTW6MoEh

N.B. -DNDEBUG has no effect on anything in libstdc++, it's not allowed to use
<assert.h> anywhere.

>      void test_copy(const std::shared_ptr<int> &sp)
>      { auto copy = sp; }
> 
> vs.
> 
>      // https://godbolt.org/z/3aoGq1f9P
>      void test_copy(const boost::shared_ptr<int> &sp)
>      { auto copy = sp; }
> 
> In the first cast, over 70 lines of assembler are emitted, in the second,
> around 30. This seems to be in large part because in
> _Sp_counted_base::_M_add_ref_copy(), you're using __atomic_add_dispatch()
> even if _Lp is _S_atomic.

That's by design:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#shared_ptr.policy
"For all three policies, reference count increments and decrements are done via
the functions in ext/atomicity.h, which detect if the program is
multi-threaded. If only one thread of execution exists in the program then less
expensive non-atomic operations are used."

> It seems to me that a specialisation of this
> function template for _S_atomic calling just __atomic_add() is missing:

No, the _S_atomic policy doesn't mean "use atomics unconditionally". We still
go through __atomic_add_dispatch so that we avoid the atomics in
single-threaded programs.

With a new glibc the check for multiple threads is cheaper and should optimize
better.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation
  2021-07-10 20:51 [Bug libstdc++/101406] New: shared_ptr in _S_atomic mode still uses __atomic_add_dispatch() marc.mutz at kdab dot com
  2021-07-11  8:52 ` [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation marc.mutz at kdab dot com
  2021-07-12 10:44 ` redi at gcc dot gnu.org
@ 2021-07-12 14:21 ` marc.mutz at kdab dot com
  2 siblings, 0 replies; 4+ messages in thread
From: marc.mutz at kdab dot com @ 2021-07-12 14:21 UTC (permalink / raw)
  To: gcc-bugs

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

Marc Mutz <marc.mutz at kdab dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|WONTFIX                     |FIXED

--- Comment #3 from Marc Mutz <marc.mutz at kdab dot com> ---
Ok, it doesn't seem to affect execution speed much, but oh my, the code size :/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-12 14:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-10 20:51 [Bug libstdc++/101406] New: shared_ptr in _S_atomic mode still uses __atomic_add_dispatch() marc.mutz at kdab dot com
2021-07-11  8:52 ` [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation marc.mutz at kdab dot com
2021-07-12 10:44 ` redi at gcc dot gnu.org
2021-07-12 14:21 ` marc.mutz at kdab dot com

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).