From 044e57922486cc2785c6a2707afce73a2ed78a86 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 7 Jul 2024 13:44:28 +0300 Subject: [PATCH 1/6] Fewer allocations to put smaller bitset to stream --- stl/inc/bitset | 54 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/stl/inc/bitset b/stl/inc/bitset index 66ec4c06c4b..169c75b76ba 100644 --- a/stl/inc/bitset +++ b/stl/inc/bitset @@ -357,26 +357,7 @@ public: // convert bitset to string basic_string<_Elem, _Tr, _Alloc> _Str; _Str._Resize_and_overwrite(_Bits, [this, _Elem0, _Elem1](_Elem* _Buf, size_t _Len) { -#if _USE_STD_VECTOR_ALGORITHMS - constexpr size_t _Bitset_vector_threshold = 32; - if constexpr (_Bits >= _Bitset_vector_threshold && is_integral_v<_Elem> && sizeof(_Elem) <= 2) { - if (!_Is_constant_evaluated()) { - if constexpr (sizeof(_Elem) == 1) { - __std_bitset_to_string_1(reinterpret_cast(_Buf), _Array, _Len, static_cast(_Elem0), - static_cast(_Elem1)); - } else { - _STL_INTERNAL_STATIC_ASSERT(sizeof(_Elem) == 2); - __std_bitset_to_string_2(reinterpret_cast(_Buf), _Array, _Len, - static_cast(_Elem0), static_cast(_Elem1)); - } - return _Len; - } - } -#endif // _USE_STD_VECTOR_ALGORITHMS - - for (size_t _Pos = 0; _Pos < _Len; ++_Pos) { - _Buf[_Pos] = _Subscript(_Len - 1 - _Pos) ? _Elem1 : _Elem0; - } + _To_string(_Buf, _Len, _Elem0, _Elem1); return _Len; }); return _Str; @@ -473,6 +454,31 @@ public: return _Array[_Wpos]; } + template + _CONSTEXPR23 void _To_string( + _Elem* const _Buf, const size_t _Len, const _Elem _Elem0, const _Elem _Elem1) const noexcept { +#if _USE_STD_VECTOR_ALGORITHMS + constexpr size_t _Bitset_vector_threshold = 32; + if constexpr (_Bits >= _Bitset_vector_threshold && is_integral_v<_Elem> && sizeof(_Elem) <= 2) { + if (!_Is_constant_evaluated()) { + if constexpr (sizeof(_Elem) == 1) { + __std_bitset_to_string_1(reinterpret_cast(_Buf), _Array, _Len, static_cast(_Elem0), + static_cast(_Elem1)); + } else { + _STL_INTERNAL_STATIC_ASSERT(sizeof(_Elem) == 2); + __std_bitset_to_string_2(reinterpret_cast(_Buf), _Array, _Len, + static_cast(_Elem0), static_cast(_Elem1)); + } + } + } else +#endif // _USE_STD_VECTOR_ALGORITHMS + { + for (size_t _Pos = 0; _Pos < _Len; ++_Pos) { + _Buf[_Pos] = _Subscript(_Len - 1 - _Pos) ? _Elem1 : _Elem0; + } + } + } + private: friend hash>; @@ -547,7 +553,13 @@ basic_ostream<_Elem, _Tr>& operator<<(basic_ostream<_Elem, _Tr>& _Ostr, const bi const _Elem _Elem0 = _Ctype_fac.widen('0'); const _Elem _Elem1 = _Ctype_fac.widen('1'); - return _Ostr << _Right.template to_string<_Elem, _Tr, allocator<_Elem>>(_Elem0, _Elem1); + if constexpr (constexpr size_t _Stack_reservation_bytes = 128; _Bits * sizeof(_Elem) <= _Stack_reservation_bytes) { + _Elem _Buf[_Bits]; + _Right._To_string(_Buf, _Elem0, _Elem1); + return _Ostr << _Buf; + } else { + return _Ostr << _Right.template to_string<_Elem, _Tr, allocator<_Elem>>(_Elem0, _Elem1); + } } _EXPORT_STD template From a2f068fef5642b0d2240b18469c2491aa6b0d267 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 7 Jul 2024 14:00:43 +0300 Subject: [PATCH 2/6] fix all bugs --- stl/inc/bitset | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/stl/inc/bitset b/stl/inc/bitset index 169c75b76ba..9570419d0d9 100644 --- a/stl/inc/bitset +++ b/stl/inc/bitset @@ -470,12 +470,11 @@ public: static_cast(_Elem0), static_cast(_Elem1)); } } - } else + } #endif // _USE_STD_VECTOR_ALGORITHMS - { - for (size_t _Pos = 0; _Pos < _Len; ++_Pos) { - _Buf[_Pos] = _Subscript(_Len - 1 - _Pos) ? _Elem1 : _Elem0; - } + + for (size_t _Pos = 0; _Pos < _Len; ++_Pos) { + _Buf[_Pos] = _Subscript(_Len - 1 - _Pos) ? _Elem1 : _Elem0; } } @@ -554,8 +553,9 @@ basic_ostream<_Elem, _Tr>& operator<<(basic_ostream<_Elem, _Tr>& _Ostr, const bi const _Elem _Elem1 = _Ctype_fac.widen('1'); if constexpr (constexpr size_t _Stack_reservation_bytes = 128; _Bits * sizeof(_Elem) <= _Stack_reservation_bytes) { - _Elem _Buf[_Bits]; - _Right._To_string(_Buf, _Elem0, _Elem1); + _Elem _Buf[_Bits + 1]; + _Right._To_string(_Buf, _Bits, _Elem0, _Elem1); + _Buf[_Bits] = _Elem{'\0'}; return _Ostr << _Buf; } else { return _Ostr << _Right.template to_string<_Elem, _Tr, allocator<_Elem>>(_Elem0, _Elem1); From 7b7d742fee161edf505ff892c0044cac1c1d5568 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 7 Jul 2024 14:41:04 +0300 Subject: [PATCH 3/6] define reservation --- stl/inc/bitset | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stl/inc/bitset b/stl/inc/bitset index 9570419d0d9..f11587c205d 100644 --- a/stl/inc/bitset +++ b/stl/inc/bitset @@ -18,6 +18,10 @@ _STL_DISABLE_CLANG_WARNINGS #pragma push_macro("new") #undef new +#ifndef _STD_BITSET_TO_STRREAM_STACK_RESERVATION +#define _STD_BITSET_TO_STRREAM_STACK_RESERVATION 128 +#endif // !defined(_STD_BITSET_TO_STRREAM_STACK_RESERVATION) + #if _USE_STD_VECTOR_ALGORITHMS extern "C" { __declspec(noalias) void __stdcall __std_bitset_to_string_1( @@ -552,7 +556,7 @@ basic_ostream<_Elem, _Tr>& operator<<(basic_ostream<_Elem, _Tr>& _Ostr, const bi const _Elem _Elem0 = _Ctype_fac.widen('0'); const _Elem _Elem1 = _Ctype_fac.widen('1'); - if constexpr (constexpr size_t _Stack_reservation_bytes = 128; _Bits * sizeof(_Elem) <= _Stack_reservation_bytes) { + if constexpr (_Bits * sizeof(_Elem) <= _STD_BITSET_TO_STRREAM_STACK_RESERVATION) { _Elem _Buf[_Bits + 1]; _Right._To_string(_Buf, _Bits, _Elem0, _Elem1); _Buf[_Bits] = _Elem{'\0'}; From d153c3ad03338d95d44d642fd288bb22ae1f8673 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 7 Jul 2024 14:55:39 +0300 Subject: [PATCH 4/6] missing return --- stl/inc/bitset | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stl/inc/bitset b/stl/inc/bitset index f11587c205d..3dd236abe01 100644 --- a/stl/inc/bitset +++ b/stl/inc/bitset @@ -473,6 +473,8 @@ public: __std_bitset_to_string_2(reinterpret_cast(_Buf), _Array, _Len, static_cast(_Elem0), static_cast(_Elem1)); } + + return; } } #endif // _USE_STD_VECTOR_ALGORITHMS From 5934ad5b7fabd6f0c210ea53b0ebc6b899587d36 Mon Sep 17 00:00:00 2001 From: Alex Guteniev Date: Sun, 7 Jul 2024 14:55:50 +0300 Subject: [PATCH 5/6] typo --- stl/inc/bitset | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stl/inc/bitset b/stl/inc/bitset index 3dd236abe01..746b3b956ca 100644 --- a/stl/inc/bitset +++ b/stl/inc/bitset @@ -18,9 +18,9 @@ _STL_DISABLE_CLANG_WARNINGS #pragma push_macro("new") #undef new -#ifndef _STD_BITSET_TO_STRREAM_STACK_RESERVATION -#define _STD_BITSET_TO_STRREAM_STACK_RESERVATION 128 -#endif // !defined(_STD_BITSET_TO_STRREAM_STACK_RESERVATION) +#ifndef _STD_BITSET_TO_STREAM_STACK_RESERVATION +#define _STD_BITSET_TO_STREAM_STACK_RESERVATION 128 +#endif // !defined(_STD_BITSET_TO_STREAM_STACK_RESERVATION) #if _USE_STD_VECTOR_ALGORITHMS extern "C" { @@ -558,7 +558,7 @@ basic_ostream<_Elem, _Tr>& operator<<(basic_ostream<_Elem, _Tr>& _Ostr, const bi const _Elem _Elem0 = _Ctype_fac.widen('0'); const _Elem _Elem1 = _Ctype_fac.widen('1'); - if constexpr (_Bits * sizeof(_Elem) <= _STD_BITSET_TO_STRREAM_STACK_RESERVATION) { + if constexpr (_Bits * sizeof(_Elem) <= _STD_BITSET_TO_STREAM_STACK_RESERVATION) { _Elem _Buf[_Bits + 1]; _Right._To_string(_Buf, _Bits, _Elem0, _Elem1); _Buf[_Bits] = _Elem{'\0'}; From d6613ef4cc1d1bae0b2abc9adc99661a13a6a93f Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 8 Jul 2024 09:58:12 -0700 Subject: [PATCH 6/6] `_Elem` doesn't need a default argument. --- stl/inc/bitset | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/bitset b/stl/inc/bitset index 746b3b956ca..f7eba3f7c7a 100644 --- a/stl/inc/bitset +++ b/stl/inc/bitset @@ -458,7 +458,7 @@ public: return _Array[_Wpos]; } - template + template _CONSTEXPR23 void _To_string( _Elem* const _Buf, const size_t _Len, const _Elem _Elem0, const _Elem _Elem1) const noexcept { #if _USE_STD_VECTOR_ALGORITHMS