From patchwork Fri Sep 30 20:57:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1612 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp667905wrs; Fri, 30 Sep 2022 13:59:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5FiClHmUq826M+T0LQrGmrWj2IUNt93WgXanB2wc8VeonP7fxswyApxZ447sYV2lSk9OWs X-Received: by 2002:a17:907:94c1:b0:783:59a:da2d with SMTP id dn1-20020a17090794c100b00783059ada2dmr7649637ejc.136.1664571560020; Fri, 30 Sep 2022 13:59:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664571560; cv=none; d=google.com; s=arc-20160816; b=M2e9Sdlar7PsUJCPOdjCzEvkSez4IIPgTjCt3vfufRY9Mv1TE09CbBnaHq8QfMVSF/ fUosjTL5cXGyyCFZxDOxkFB9R+NZHPP5Yld5rnXrjbC8vKnqp1yOw/rUromSfSP1hAm/ Z7O24Tb+qfpB0NVMk/CNp5899pGwQf7WNyc4IQtq/fLFD6utVg8Oj750UmTo6j8L7r5p exTX0YwhGaSFKJiUiiHu4uqt4eB3CPy5KorLCL1HKIngGFRckud+aCm2ZiN3IqQ0quv6 tUaMgjE1d3qlZ2q09duwvsAmnkafrOXL1amlqBslu/J/N/6CI8AyZvSW5VH1yHtAM5Fa Fx/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=8YVNY8sAnOps0NFAJ+SFKP2pqMqtPHzk7l6Jkk221fY=; b=0IozDFlmusC9guwnePpuYc1qzWmROFpUWyuejfBDJnHZXW1X4A3LYap8zkFXD0o8Hn SuRACvCVu3LC7CMsqMbkjzs+KJk0ct3pKU/8eoN8P/Pe7hgsPwfMZWPaHPfjmDTjIfXy urLM7FV/sSpeXgkOEOG5nCm23cmNRH8LIljD/XXaFe8JDga9mnGBE6ZFIV/3T6ubL1R+ 1ETGSns6hWFcV4h0Ie36+mFyGjzEEWhXvMpRli1L4XTUipz+Yn/abIIx/lcUTi2aP1vi AwXZxKDQd2KzpK45dqj8YGlMhfNfF4T4h6W6AqFtx1JJ/oO61rygDMpLGn/m0aA13wGG 7qdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qR6uIjwn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dt17-20020a170907729100b00780f525c55csi2394087ejc.48.2022.09.30.13.59.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Sep 2022 13:59:20 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qR6uIjwn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D25193853568 for ; Fri, 30 Sep 2022 20:58:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D25193853568 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664571524; bh=8YVNY8sAnOps0NFAJ+SFKP2pqMqtPHzk7l6Jkk221fY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=qR6uIjwn8POLAIWs9AU0oCo9GDZ+eMAzSMrMU/vDHNKjsO5i7OuZhA7ukF3bSHP9Y 0sGVjWqMRThGu/4/glhkPSW2XJt5T7t4UP/Ru8GfWlCpp+rcm5gffoztP6YSagxVhw FRxxnvWOD7vC1J7T522w2hCBXFHXwdMnLdsUZ7t0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 4334C3857BA6 for ; Fri, 30 Sep 2022 20:57:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4334C3857BA6 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) 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745429788547915617?= X-GMAIL-MSGID: =?utf-8?q?1745429788547915617?= 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;