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++: Optimize std::bitset<N>::to_string
Date: Fri, 23 Sep 2022 12:58:38 +0100	[thread overview]
Message-ID: <20220923115838.1327654-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk.

-- >8 --

This makes to_string approximately twice as fast at any optimization
level. Instead of iterating through every bit, jump straight to the next
bit that is set, by using _Find_first and _Find_next.

libstdc++-v3/ChangeLog:

	* include/std/bitset (bitset::_M_copy_to_string): Find set bits
	instead of iterating over individual bits.
---
 libstdc++-v3/include/std/bitset | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset
index 0c84f15fda0..83c6416b770 100644
--- a/libstdc++-v3/include/std/bitset
+++ b/libstdc++-v3/include/std/bitset
@@ -1495,9 +1495,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 			_CharT __zero, _CharT __one) const
       {
 	__s.assign(_Nb, __zero);
-	for (size_t __i = _Nb; __i > 0; --__i)
-	  if (_Unchecked_test(__i - 1))
-	    _Traits::assign(__s[_Nb - __i], __one);
+	size_t __n = this->_Find_first();
+	while (__n < _Nb)
+	  {
+	    __s[_Nb - __n - 1] = __one;
+	    __n = _Find_next(__n);
+	  }
       }
 #endif // HOSTED
 
-- 
2.37.3


                 reply	other threads:[~2022-09-23 11:58 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=20220923115838.1327654-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).