]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Optimize std::bitset<N>::to_string
authorJonathan Wakely <jwakely@redhat.com>
Thu, 22 Sep 2022 17:36:04 +0000 (18:36 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 23 Sep 2022 11:53:51 +0000 (12:53 +0100)
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

index 0c84f15fda0e6908018aa1054686ca70a20d0712..83c6416b7706292a72c304c6d2ec7fa72d8a50df 100644 (file)
@@ -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
 
This page took 0.063628 seconds and 5 git commands to generate.