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 08F8B3858C52 for ; Fri, 23 Sep 2022 11:58:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 08F8B3858C52 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=1663934321; 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=C7U2hUwJCvp3ZmudEunbt9bfkv5Y0DWvZq7IeEkV6ZY=; b=CBuwSMvqBYuceYYWwz8Vy7kXXIuzqoaNJfjO4RPz1ww28XdWcuawVC3F+zo7OkDRmivCyH CMl4hbeqxELGaNhQ30lgyhioUpob1I8VbE3Hawz3gJYvy8t4uF58VwVrtH8XF/98aj3qDn lEf7h1smKNnYDG8D9w3ZqJ883zrFAKc= 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) From: Jonathan Wakely 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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=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 -- 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 -- 2.37.3