libstdc++: Optimize operator>> for std::bitset
authorJonathan Wakely <jwakely@redhat.com>
Fri, 30 Sep 2022 16:06:15 +0000 (17:06 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 30 Sep 2022 20:52:02 +0000 (21:52 +0100)
commit1c12a3cfdfabf67c7962ee6532e001e4d48e7fc2
treea7097f8c7d1f1181f079e1dfd43951c73d132223
parent4eb46f453cc74adf0055dae35cec41f4a4c4be5b
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.
libstdc++-v3/include/std/bitset