public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-9344] libstdc++: Optimize std::bitset<N>::to_string
@ 2023-03-28 23:35 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-03-28 23:35 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:33ff7cb3386fb9477f145fa4ab74bdf70d0cb2f3

commit r12-9344-g33ff7cb3386fb9477f145fa4ab74bdf70d0cb2f3
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Sep 22 18:36:04 2022 +0100

    libstdc++: Optimize std::bitset<N>::to_string
    
    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.
    
    (cherry picked from commit ffb03fa12850df3a4f53435d5f20ff122c83732a)

Diff:
---
 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 438c2f7efe9..8ff9d7fdefd 100644
--- a/libstdc++-v3/include/std/bitset
+++ b/libstdc++-v3/include/std/bitset
@@ -1415,9 +1415,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);
+	  }
       }
 
   // 23.3.5.3 bitset operations:

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-28 23:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-28 23:35 [gcc r12-9344] libstdc++: Optimize std::bitset<N>::to_string Jonathan Wakely

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).