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: 57964 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9B91C385703A for ; Fri, 23 Sep 2022 11:59:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B91C385703A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663934353; bh=C7U2hUwJCvp3ZmudEunbt9bfkv5Y0DWvZq7IeEkV6ZY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=WDZyGHzhQs2xIjN3QHxCbSrsMw+FvnQiYOMeuag/0/elMviQ163oWiP4x/+M7vQZG GtQ+qGIkzAH+TZA8YodEW/Uvryd6yie1sHipZ/oRecWTvZE3bjgtyO1k/dGUrGLgcN 6BCdIYNtT+72Jb9yqR3ImMMhM4Enl1oMb17yAQx0= 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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