public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-2998] libstdc++: Optimize operator>> for std::bitset
Date: Fri, 30 Sep 2022 20:56:55 +0000 (GMT)	[thread overview]
Message-ID: <20220930205656.750633858430@sourceware.org> (raw)

https://gcc.gnu.org/g:1c12a3cfdfabf67c7962ee6532e001e4d48e7fc2

commit r13-2998-g1c12a3cfdfabf67c7962ee6532e001e4d48e7fc2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 30 17:06:15 2022 +0100

    libstdc++: Optimize operator>> for std::bitset
    
    We can improve performance by using a char buffer instead of
    basic_string. The loop bound already means we can't overflow the buffer,
    and we don't need to keep writing a null character after every character
    written to the buffer.
    
    We could just use basic_string::resize(N) to zero-init the whole string,
    then overwrite those chars. But that zero-init of all N chars would be
    wasted in the case where we are writing to a bitset<N> with large N, but
    only end up extracting one or two chars from the stream.
    
    With this change we just use buffer of uninitialized chars.  For a
    small-ish bitset (currently <= 256) we can improve performance further
    by using alloca instead of the heap.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/bitset (operator>>): Use a simple buffer instead
            of std::basic_string.

Diff:
---
 libstdc++-v3/include/std/bitset | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset
index 757da020ffe..1038cc65138 100644
--- a/libstdc++-v3/include/std/bitset
+++ b/libstdc++-v3/include/std/bitset
@@ -1563,8 +1563,22 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       typedef std::basic_istream<_CharT, _Traits>  __istream_type;
       typedef typename __istream_type::ios_base    __ios_base;
 
-      std::basic_string<_CharT, _Traits> __tmp;
-      __tmp.reserve(_Nb);
+      struct _Buffer
+      {
+	_Buffer()
+	: _M_base(_Nb > 256 ? new _CharT[_Nb] : (_CharT*)__builtin_alloca(_Nb))
+	{ }
+
+	~_Buffer()
+	{
+	  if _GLIBCXX17_CONSTEXPR (_Nb > 256)
+	    delete[] _M_base;
+	}
+
+	_CharT* const _M_base;
+      };
+      _Buffer __buf;
+      _CharT* __ptr = __buf._M_base;
 
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 303. Bitset input operator underspecified
@@ -1591,9 +1605,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 		    {
 		      const char_type __c2 = _Traits::to_char_type(__c1);
 		      if (_Traits::eq(__c2, __zero))
-			__tmp.push_back(__zero);
+			*__ptr++ = __zero;
 		      else if (_Traits::eq(__c2, __one))
-			__tmp.push_back(__one);
+			*__ptr++ = __one;
 		      else if (_Traits::
 			       eq_int_type(__is.rdbuf()->sputbackc(__c2),
 					   __eof))
@@ -1613,11 +1627,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	    { __is._M_setstate(__ios_base::badbit); }
 	}
 
-      if (__tmp.empty() && _Nb)
-	__state |= __ios_base::failbit;
-      else if _GLIBCXX17_CONSTEXPR (_Nb)
-	__x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
-				__zero, __one);
+      if _GLIBCXX17_CONSTEXPR (_Nb)
+      {
+	if (size_t __len = __ptr - __buf._M_base)
+	  __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_base, __len,
+							 0, __len,
+							 __zero, __one);
+	else
+	  __state |= __ios_base::failbit;
+      }
       if (__state)
 	__is.setstate(__state);
       return __is;

                 reply	other threads:[~2022-09-30 20:56 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=20220930205656.750633858430@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@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).