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

> > +           // 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....
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.

Thanks,
Honza

  reply	other threads:[~2023-11-20 15:44 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 [this message]
2023-11-20 16:46     ` Jonathan Wakely
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=ZVt+52YWOyDHcBOp@kam.mff.cuni.cz \
    --to=hubicka@ucw.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --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).