public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Avoid try-catch and O(N) size in std::list::merge for old ABI
Date: Wed,  3 Aug 2022 13:31:36 +0100	[thread overview]
Message-ID: <20220803123136.589621-1-jwakely@redhat.com> (raw)

Tested x86_64-linux, pushed to gcc-10 branch.

-- >8 --

The current std::list::merge code calls size() before starting to merge
any elements, so that the _M_size members can be updated after the merge
finishes. The work is done in a try-block so that the sizes can still be
updated in an exception handler if any element comparison throws.

The _M_size members only exist for the cxx11 ABI, so the initial call to
size() and the try-catch are only needed for that ABI. For the old ABI
the size() call performs an O(N) list traversal to get a value that
isn't even used, and catching exceptions just to rethrow them isn't
needed either.

In r11-10123 this code was refactored to use an RAII guard type, but for
the gcc-10 branch a less invasive change using preprocessor conditionals
seems more appropriate.

libstdc++-v3/ChangeLog:

	* include/bits/list.tcc (list::merge) [!USE_CXX11_ABI]: Remove
	call to size() and try-catch block.
---
 libstdc++-v3/include/bits/list.tcc | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libstdc++-v3/include/bits/list.tcc b/libstdc++-v3/include/bits/list.tcc
index ce9e983c539..1f27d8b786a 100644
--- a/libstdc++-v3/include/bits/list.tcc
+++ b/libstdc++-v3/include/bits/list.tcc
@@ -406,8 +406,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	  iterator __last1 = end();
 	  iterator __first2 = __x.begin();
 	  iterator __last2 = __x.end();
+#if _GLIBCXX_USE_CXX11_ABI
 	  const size_t __orig_size = __x.size();
 	  __try {
+#endif
 	    while (__first1 != __last1 && __first2 != __last2)
 	      if (*__first2 < *__first1)
 		{
@@ -422,6 +424,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
 	    this->_M_inc_size(__x._M_get_size());
 	    __x._M_set_size(0);
+#if _GLIBCXX_USE_CXX11_ABI
 	  }
 	  __catch(...)
 	    {
@@ -430,6 +433,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	      __x._M_set_size(__dist);
 	      __throw_exception_again;
 	    }
+#endif
 	}
     }
 
-- 
2.37.1


                 reply	other threads:[~2022-08-03 12:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220803123136.589621-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --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).