From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 19EA63858298 for ; Fri, 30 Sep 2022 20:57:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 19EA63858298 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664571437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8YVNY8sAnOps0NFAJ+SFKP2pqMqtPHzk7l6Jkk221fY=; b=holPqnrgz730gRZsLKqWLCypWRdWeGPv83/F9pGx96xL/ECU2WN0OOVkR7xVLqKjNfM0NV +lVT3zpyGPeQif7RR14QWTYtxVQcPzoF+iQj70Ok0tx9ppAAR96UIQXXc3BNattVVFISiN PQsc5qWwgm9iVOoL4HsccJd1SEELavg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-149-XA_ovCLPOhmm36V6stX-cA-1; Fri, 30 Sep 2022 16:57:14 -0400 X-MC-Unique: XA_ovCLPOhmm36V6stX-cA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4AAE9185A794; Fri, 30 Sep 2022 20:57:14 +0000 (UTC) Received: from localhost (unknown [10.33.36.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 11BE62084836; Fri, 30 Sep 2022 20:57:13 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Optimize operator>> for std::bitset Date: Fri, 30 Sep 2022 21:57:13 +0100 Message-Id: <20220930205713.170346-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. -- >8 -- 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 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 | 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(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; -- 2.37.3