public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Jan Hubicka <hubicka@ucw.cz>
Cc: Jonathan Wakely <jwakely@redhat.com>,
	libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: libstdc++: Speed up push_back
Date: Mon, 20 Nov 2023 16:46:34 +0000	[thread overview]
Message-ID: <CAH6eHdQcVSyJPjawvBngTBC-Vux5JA-sR_zXUV+6dAR02hPm1g@mail.gmail.com> (raw)
In-Reply-To: <ZVt+52YWOyDHcBOp@kam.mff.cuni.cz>

On Mon, 20 Nov 2023 at 15:44, Jan Hubicka <hubicka@ucw.cz> wrote:
>
> > > +           // RAII type to destroy initialized elements.
> >
> > There's only one initialized element, not "elements".
> >
> > > +           struct _Guard_elts
> > > +           {
> > > +             pointer _M_first, _M_last;  // Elements to destroy
> >
> > We only need to store one pointer here, call it _M_p.
> >
> > > +             _Tp_alloc_type& _M_alloc;
> > > +
> > > +             _GLIBCXX20_CONSTEXPR
> > > +             _Guard_elts(pointer __elt, _Tp_alloc_type& __a)
> > > +             : _M_first(__elt), _M_last(__elt + 1), _M_alloc(__a)
> > > +             { }
> > > +
> > > +             _GLIBCXX20_CONSTEXPR
> > > +             ~_Guard_elts()
> > > +             { std::_Destroy(_M_first, _M_last, _M_alloc); }
> >
> > This should be either:
> >
> >   std::_Destroy(_M_p, _M_p+1, _M_alloc);
> >
> > or avoid the loop that happens in that _Destroy function:
> >
> >   _Alloc_traits::destroy(_M_alloc, _M_p);
> >
> > > +
> > > +           private:
> > > +             _Guard_elts(const _Guard_elts&);
> > > +           };
> > > +
> > > +           // Guard the new element so it will be destroyed if anything throws.
> > > +           _Guard_elts __guard_elts(__new_start + __elems, _M_impl);
> > > +
> > > +           __new_finish = std::__uninitialized_move_if_noexcept_a(
> > > +                            __old_start, __old_finish,
> > > +                            __new_start, _M_get_Tp_allocator());
> > > +
> > > +           ++__new_finish;
> > > +           // Guard everything before the new element too.
> > > +           __guard_elts._M_first = __new_start;
> >
> > This seems redundant, we're not doing any more insertions now, and so
> > this store is dead.
>
> I removed this one.
> >
> > > +
> > > +           // New storage has been fully initialized, destroy the old elements.
> > > +           __guard_elts._M_first = __old_start;
> > > +           __guard_elts._M_last = __old_finish;
>
> However here I think I need __guard_elts supporting destruction of many
> elements in case the vector has moved to new location....

Ah yes.

> So I do not quite  see how to simplify the code as suggested above
> except that the constructor can be simplified to not require first and
> last argument since we always initialize it for 1 destruction but later
> we may update it.

We could just destroy the old ones manually at the end of this block,
it doesn't need to be done by the guard, e.g. update the guard to
destroy the first one, then loop to destroy the others:

__guard_elt._M_p = __old_start++;
std::_Destroy(__old_start, __old_finish, this->_M_impl);

But this would only improve codegen in the exceptional case when
something throws, and the guard destructor destroys a single element.
For the common case where we reach the end of the block, we're always
going to loop. So I agree with leaving __guard_elts in charge of two
pointers, and looping in its dtor.

So OK for trunk, with the dead store removed. Thanks!

  reply	other threads:[~2023-11-20 16:46 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-19 21:53 Jan Hubicka
2023-11-20 12:09 ` Jonathan Wakely
2023-11-20 15:44   ` Jan Hubicka
2023-11-20 16:46     ` Jonathan Wakely [this message]
2023-11-21 12:50   ` Jan Hubicka
2023-11-21 13:07     ` Jonathan Wakely
2023-11-23  8:15 ` Matthias Kretz
2023-11-23 15:07   ` Jan Hubicka
2023-11-23 15:33     ` Jan Hubicka
2023-11-23 15:43       ` Jan Hubicka
2023-11-23 16:26         ` Jonathan Wakely
2023-11-23 16:20       ` Jonathan Wakely
2023-11-24 10:21         ` Martin Jambor
2023-11-24 10:23           ` Richard Biener
2023-11-24 19:45         ` Marc Glisse
2023-11-24 20:07     ` Jan Hubicka
2023-11-24 21:55       ` Jonathan Wakely

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=CAH6eHdQcVSyJPjawvBngTBC-Vux5JA-sR_zXUV+6dAR02hPm1g@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hubicka@ucw.cz \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@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).