public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/101406] poor codegen for shared_ptr copy & destory, compared to boost::shared_ptr and libc++'s implementation
Date: Mon, 12 Jul 2021 10:44:50 +0000	[thread overview]
Message-ID: <bug-101406-4-kXSF8Q3zkp@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-101406-4@http.gcc.gnu.org/bugzilla/>

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.

  parent reply	other threads:[~2021-07-12 10:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2021-07-12 14:21 ` marc.mutz at kdab dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-101406-4-kXSF8Q3zkp@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).