From patchwork Fri Sep 23 11:58:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1415 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp177659wrt; Fri, 23 Sep 2022 04:59:40 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7hhlgeyvBdCuv0S0TyF9qYnHF9DQSCPTyHXT1QiDQr2aY2yiUrzsJJl5uyYHtvA+319+hb X-Received: by 2002:aa7:c61a:0:b0:44e:7d1d:7814 with SMTP id h26-20020aa7c61a000000b0044e7d1d7814mr7991534edq.44.1663934380189; Fri, 23 Sep 2022 04:59:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663934380; cv=none; d=google.com; s=arc-20160816; b=Bp3mzH9Si8vlxinko5VeraYWoI4tWxlHTzDx5+BefqJhmVA+zTXhpTpCmYvB6ANyGt oB+gN2zX5oKWHv6Z50O87xwNNqx/FxLXV7n84tN7R/Bp+7FmR3mXkykvxm1BqhXKX2Bi LqSE1kPGQr3kbupqI9i6xkNi2Yb2Fn2Nw0GbGR4fx3tgK1akyBA9+lPT49VNFiHiGmqK 9YGE9mipos6ZiM8+tWe7Z3bMeNhhXjyrRIFt+gqXK5cr+y96Uy1UD4S6qo1tSIe5LU/W aEGiRrnm97fcSPY7hzkjge/Ps1M+tUYqcqHRFmcIy7LNaCDIgQYj7y2699LbbIWs1DQW 3nKg== 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=C7U2hUwJCvp3ZmudEunbt9bfkv5Y0DWvZq7IeEkV6ZY=; b=kUiqRLfCFfUKIcabBzSiYB+kHPjHZld9HjGys91iVYh0eECmluuyNGDFrjHHlGGKJL nSlRm+XvgMe8Jiwmb57ICYZ6OPLHMsabywIw8/erVC+evWMaM7b+0S9wu+yCI+BIrMly 2ectMH5rGaW7RyGrA1Gi7APWkC8I/6DoWkLGWW2zo9+6tXLvHErdKRxZegW72w0nMumD uLopknMfh1CydQlt4VHBeXT39YJJSasxE0oEUJbJgzFY6I3+gU27x+DiP+qRZvdHW11Y 4mqboKC1pk6R2eP+pqEztM5QVbIyQOpLU5JuIvHypYe0QfbLWy4K/Ew/8eVQCxB8Otj5 4mSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Zg0F9j2+; 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 oz32-20020a1709077da000b0077077ded253si6982067ejc.924.2022.09.23.04.59.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Sep 2022 04:59:40 -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=Zg0F9j2+; 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 BADA53857373 for ; Fri, 23 Sep 2022 11:59:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BADA53857373 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663934367; bh=C7U2hUwJCvp3ZmudEunbt9bfkv5Y0DWvZq7IeEkV6ZY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Zg0F9j2+xoeZIAS9ShVhUOIPeb3hfhxqoCqpdyfBa0qjwK06OOiDPEhdUtGmhpBX9 KyR5gH9r+icwcDaIqLWR9XwS8gBMvERtdesvVUdEeagQ4lBqMDZaf3+fOjsqtZ7aDs Ua862rc3BUVyRb6WLh7OqM6sdf0m07fupCicorJs= 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 B9944385828B for ; Fri, 23 Sep 2022 11:58:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B9944385828B 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-563-kb5WABeZPJGLc-4PHRYUaA-1; Fri, 23 Sep 2022 07:58:40 -0400 X-MC-Unique: kb5WABeZPJGLc-4PHRYUaA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A8F1485A5B6; Fri, 23 Sep 2022 11:58:39 +0000 (UTC) Received: from localhost (unknown [10.33.36.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 72DCC4B3FCF; Fri, 23 Sep 2022 11:58:39 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Optimize std::bitset::to_string Date: Fri, 23 Sep 2022 12:58:38 +0100 Message-Id: <20220923115838.1327654-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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=unavailable 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?1744761656892344621?= X-GMAIL-MSGID: =?utf-8?q?1744761656892344621?= Tested x86_64-linux. Pushed to trunk. -- >8 -- This makes to_string approximately twice as fast at any optimization level. Instead of iterating through every bit, jump straight to the next bit that is set, by using _Find_first and _Find_next. libstdc++-v3/ChangeLog: * include/std/bitset (bitset::_M_copy_to_string): Find set bits instead of iterating over individual bits. --- libstdc++-v3/include/std/bitset | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/std/bitset b/libstdc++-v3/include/std/bitset index 0c84f15fda0..83c6416b770 100644 --- a/libstdc++-v3/include/std/bitset +++ b/libstdc++-v3/include/std/bitset @@ -1495,9 +1495,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _CharT __zero, _CharT __one) const { __s.assign(_Nb, __zero); - for (size_t __i = _Nb; __i > 0; --__i) - if (_Unchecked_test(__i - 1)) - _Traits::assign(__s[_Nb - __i], __one); + size_t __n = this->_Find_first(); + while (__n < _Nb) + { + __s[_Nb - __n - 1] = __one; + __n = _Find_next(__n); + } } #endif // HOSTED