From patchwork Tue Jul 16 12:06:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1961070 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QjyyLVel; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WNdDL6LVjz1xrQ for ; Tue, 16 Jul 2024 22:09:54 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 296C7387093D for ; Tue, 16 Jul 2024 12:09:53 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 1A5883870C0E for ; Tue, 16 Jul 2024 12:07:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1A5883870C0E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1A5883870C0E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721131691; cv=none; b=A6cxsggM+vHjL/BizTr3bH9sHnJ+QreklFQWXkNiAaEid9JChnHUDOswOkV8sLsaAtvvymLASOtkaiATy3L+VzeHaZP50ZSw4TnV3qF2TDGlgdaxVqtlNlyDEztjtflj4dLaQcIhvT8+AOnlbO00ZGqmbp9E5fmVQ6R2v/IVZc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721131691; c=relaxed/simple; bh=IM1C2op82JEa3APrQu6MryuatVkeOPUXtpaXgeeDvjE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=k4HnSwWro/Ca3Li93hcwf53oZvMurniUrJRYe0s+WTeA2pGK81o9vGri17DmbMvyUrKpyc8bhDlRxzS0ss/sePceKPSRG3Y81ygcsyYCEKztwuoyoUwwBTSlt5mr6fz005yygkCcteChbN/uFlDrNsLVYjrHFlg3VQgnmmbrbtk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721131673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=i6I23BjtaAQx3ZIiwF9FE90CKOQWB0x8auktHERMxX4=; b=QjyyLVelrDRBHMoZ3zsB5YqQZio/VBzR4HYhXnRPsdOsTX5ZSAT578wtD9MF0LZLWB3IOZ YQleK1tTpzjS7cVm6dfI+1mGHKKHevOt8cRM0h34MqzXsLQhG00U3RPV+F5Hb8T3KMkfmk RcUZ0HpwbFmStx2r0BdfcvSkputVVEk= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-650-T-k-1VouPlennC40q542Bg-1; Tue, 16 Jul 2024 08:07:50 -0400 X-MC-Unique: T-k-1VouPlennC40q542Bg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0AE421944A8A; Tue, 16 Jul 2024 12:07:49 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6B16619560B2; Tue, 16 Jul 2024 12:07:48 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Tamar Christina Subject: [PATCH] libstdc++: Remove __find_if unrolling for random access iterators Date: Tue, 16 Jul 2024 13:06:16 +0100 Message-ID: <20240716120747.2136231-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.5 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org I have some follow-up patches which are enabled by this, so that other algos which use std::__find_if can benefit from the memchr optimizations recently added to std::find. Currently, they can't benefit because they use the internal __find_if and only std::find is optimized. Tested x86_64-linux. I intend to push this in the next day or two. -- >8 -- As the numbers in PR libstdc++/88545 show, the manual loop unrolling in std::__find_if doesn't actually help these days, and it prevents the compiler from auto-vectorizing. Remove the dispatching on iterator_category and just use the simple loop for all iterator categories. libstdc++-v3/ChangeLog: * include/bits/stl_algobase.h (__find_if): Remove overloads for dispatching on iterator_category. Do not unroll loop manually. * include/bits/stl_algo.h (__find_if_not): Remove iterator_category argument from __find_if call. --- libstdc++-v3/include/bits/stl_algo.h | 3 +- libstdc++-v3/include/bits/stl_algobase.h | 70 ++---------------------- 2 files changed, 5 insertions(+), 68 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index d250b2e04d4..541f588883b 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -110,8 +110,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Predicate __pred) { return std::__find_if(__first, __last, - __gnu_cxx::__ops::__negate(__pred), - std::__iterator_category(__first)); + __gnu_cxx::__ops::__negate(__pred)); } /// Like find_if_not(), but uses and updates a count of the diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index dec1e4c79d8..27f6c377ad6 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -2098,77 +2098,15 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO _GLIBCXX_END_NAMESPACE_ALGO - /// This is an overload used by find algos for the Input Iterator case. - template - _GLIBCXX20_CONSTEXPR - inline _InputIterator - __find_if(_InputIterator __first, _InputIterator __last, - _Predicate __pred, input_iterator_tag) - { - while (__first != __last && !__pred(__first)) - ++__first; - return __first; - } - - /// This is an overload used by find algos for the RAI case. - template - _GLIBCXX20_CONSTEXPR - _RandomAccessIterator - __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Predicate __pred, random_access_iterator_tag) - { - typename iterator_traits<_RandomAccessIterator>::difference_type - __trip_count = (__last - __first) >> 2; - - for (; __trip_count > 0; --__trip_count) - { - if (__pred(__first)) - return __first; - ++__first; - - if (__pred(__first)) - return __first; - ++__first; - - if (__pred(__first)) - return __first; - ++__first; - - if (__pred(__first)) - return __first; - ++__first; - } - - switch (__last - __first) - { - case 3: - if (__pred(__first)) - return __first; - ++__first; - // FALLTHRU - case 2: - if (__pred(__first)) - return __first; - ++__first; - // FALLTHRU - case 1: - if (__pred(__first)) - return __first; - ++__first; - // FALLTHRU - case 0: - default: - return __last; - } - } - + // Implementation of std::find_if, also used in std::remove_if and others. template _GLIBCXX20_CONSTEXPR inline _Iterator __find_if(_Iterator __first, _Iterator __last, _Predicate __pred) { - return __find_if(__first, __last, __pred, - std::__iterator_category(__first)); + while (__first != __last && !__pred(__first)) + ++__first; + return __first; } template