From patchwork Tue Jun 14 20:19:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1643417 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=qZ4ug2uq; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LN0K51pkXz9s5V for ; Wed, 15 Jun 2022 06:24:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B905D384F027 for ; Tue, 14 Jun 2022 20:24:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B905D384F027 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1655238262; bh=L8BZYet4GQiO4sDYh60XZ4dolMH5C8NR+jehbfaS47U=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=qZ4ug2uqKBcEuEYW2lZQKi8/w1bBvs/6t37l0bCFG30Hw09V7AqapHOHZ3jFCf186 exf3R156qlALjvXNAxVvvnULoRHpskYdvBkrlBLQsuzWH2ns0qOAL9DJ0RYUU3/5zv lTZbtVlDEjHtUv2odDppIJUqbLZq0E8cx8JbNPjg= 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 CE98B384D191 for ; Tue, 14 Jun 2022 20:19:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CE98B384D191 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-520-PvC5mmJBP9y43pdmfxOgDA-1; Tue, 14 Jun 2022 16:19:56 -0400 X-MC-Unique: PvC5mmJBP9y43pdmfxOgDA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1CBD5101AA45; Tue, 14 Jun 2022 20:19:56 +0000 (UTC) Received: from localhost (unknown [10.33.36.159]) by smtp.corp.redhat.com (Postfix) with ESMTP id C0B531121314; Tue, 14 Jun 2022 20:19:55 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Inline all basic_string::compare overloads [PR59048] Date: Tue, 14 Jun 2022 21:19:55 +0100 Message-Id: <20220614201955.1030226-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 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, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. -- >8 -- Defining the compare member functions inline allows calls to traits_type::length and std::min to be inlined, taking advantage of constant expression arguments. When not inline, the compiler prefers to use the explicit instantiation definitions in libstdc++.so and can't take advantage of constant arguments. libstdc++-v3/ChangeLog: PR libstdc++/59048 * include/bits/basic_string.h (compare): Define inline. * include/bits/basic_string.tcc (compare): Remove out-of-line definitions. * include/bits/cow_string.h (compare): Define inline. * testsuite/21_strings/basic_string/operations/compare/char/3.cc: New test. --- libstdc++-v3/include/bits/basic_string.h | 63 ++++++++++++-- libstdc++-v3/include/bits/basic_string.tcc | 85 ------------------- libstdc++-v3/include/bits/cow_string.h | 63 ++++++++++++-- .../basic_string/operations/compare/char/3.cc | 7 ++ 4 files changed, 123 insertions(+), 95 deletions(-) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/operations/compare/char/3.cc diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index f76ddf970c6..a34b3d9ed28 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -3235,7 +3235,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ _GLIBCXX20_CONSTEXPR int - compare(size_type __pos, size_type __n, const basic_string& __str) const; + compare(size_type __pos, size_type __n, const basic_string& __str) const + { + _M_check(__pos, "basic_string::compare"); + __n = _M_limit(__pos, __n); + const size_type __osize = __str.size(); + const size_type __len = std::min(__n, __osize); + int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); + if (!__r) + __r = _S_compare(__n, __osize); + return __r; + } /** * @brief Compare substring to a substring. @@ -3263,7 +3273,19 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _GLIBCXX20_CONSTEXPR int compare(size_type __pos1, size_type __n1, const basic_string& __str, - size_type __pos2, size_type __n2 = npos) const; + size_type __pos2, size_type __n2 = npos) const + { + _M_check(__pos1, "basic_string::compare"); + __str._M_check(__pos2, "basic_string::compare"); + __n1 = _M_limit(__pos1, __n1); + __n2 = __str._M_limit(__pos2, __n2); + const size_type __len = std::min(__n1, __n2); + int __r = traits_type::compare(_M_data() + __pos1, + __str.data() + __pos2, __len); + if (!__r) + __r = _S_compare(__n1, __n2); + return __r; + } /** * @brief Compare to a C string. @@ -3281,7 +3303,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ _GLIBCXX20_CONSTEXPR int - compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT; + compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT + { + __glibcxx_requires_string(__s); + const size_type __size = this->size(); + const size_type __osize = traits_type::length(__s); + const size_type __len = std::min(__size, __osize); + int __r = traits_type::compare(_M_data(), __s, __len); + if (!__r) + __r = _S_compare(__size, __osize); + return __r; + } // _GLIBCXX_RESOLVE_LIB_DEFECTS // 5 String::compare specification questionable @@ -3306,7 +3338,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ _GLIBCXX20_CONSTEXPR int - compare(size_type __pos, size_type __n1, const _CharT* __s) const; + compare(size_type __pos, size_type __n1, const _CharT* __s) const + { + __glibcxx_requires_string(__s); + _M_check(__pos, "basic_string::compare"); + __n1 = _M_limit(__pos, __n1); + const size_type __osize = traits_type::length(__s); + const size_type __len = std::min(__n1, __osize); + int __r = traits_type::compare(_M_data() + __pos, __s, __len); + if (!__r) + __r = _S_compare(__n1, __osize); + return __r; + } /** * @brief Compare substring against a character %array. @@ -3335,7 +3378,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _GLIBCXX20_CONSTEXPR int compare(size_type __pos, size_type __n1, const _CharT* __s, - size_type __n2) const; + size_type __n2) const + { + __glibcxx_requires_string_len(__s, __n2); + _M_check(__pos, "basic_string::compare"); + __n1 = _M_limit(__pos, __n1); + const size_type __len = std::min(__n1, __n2); + int __r = traits_type::compare(_M_data() + __pos, __s, __len); + if (!__r) + __r = _S_compare(__n1, __n2); + return __r; + } #if __cplusplus >= 202002L constexpr bool diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 0696b96604c..4563c61429a 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -847,91 +847,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return npos; } - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string<_CharT, _Traits, _Alloc>:: - compare(size_type __pos, size_type __n, const basic_string& __str) const - { - _M_check(__pos, "basic_string::compare"); - __n = _M_limit(__pos, __n); - const size_type __osize = __str.size(); - const size_type __len = std::min(__n, __osize); - int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); - if (!__r) - __r = _S_compare(__n, __osize); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string<_CharT, _Traits, _Alloc>:: - compare(size_type __pos1, size_type __n1, const basic_string& __str, - size_type __pos2, size_type __n2) const - { - _M_check(__pos1, "basic_string::compare"); - __str._M_check(__pos2, "basic_string::compare"); - __n1 = _M_limit(__pos1, __n1); - __n2 = __str._M_limit(__pos2, __n2); - const size_type __len = std::min(__n1, __n2); - int __r = traits_type::compare(_M_data() + __pos1, - __str.data() + __pos2, __len); - if (!__r) - __r = _S_compare(__n1, __n2); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string<_CharT, _Traits, _Alloc>:: - compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT - { - __glibcxx_requires_string(__s); - const size_type __size = this->size(); - const size_type __osize = traits_type::length(__s); - const size_type __len = std::min(__size, __osize); - int __r = traits_type::compare(_M_data(), __s, __len); - if (!__r) - __r = _S_compare(__size, __osize); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string <_CharT, _Traits, _Alloc>:: - compare(size_type __pos, size_type __n1, const _CharT* __s) const - { - __glibcxx_requires_string(__s); - _M_check(__pos, "basic_string::compare"); - __n1 = _M_limit(__pos, __n1); - const size_type __osize = traits_type::length(__s); - const size_type __len = std::min(__n1, __osize); - int __r = traits_type::compare(_M_data() + __pos, __s, __len); - if (!__r) - __r = _S_compare(__n1, __osize); - return __r; - } - - template - _GLIBCXX_STRING_CONSTEXPR - int - basic_string <_CharT, _Traits, _Alloc>:: - compare(size_type __pos, size_type __n1, const _CharT* __s, - size_type __n2) const - { - __glibcxx_requires_string_len(__s, __n2); - _M_check(__pos, "basic_string::compare"); - __n1 = _M_limit(__pos, __n1); - const size_type __len = std::min(__n1, __n2); - int __r = traits_type::compare(_M_data() + __pos, __s, __len); - if (!__r) - __r = _S_compare(__n1, __n2); - return __r; - } - #undef _GLIBCXX_STRING_CONSTEXPR // 21.3.7.9 basic_string::getline and operators diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index 7bceb06a79b..f16e33ac1ef 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -2852,7 +2852,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * the shorter one is ordered first. */ int - compare(size_type __pos, size_type __n, const basic_string& __str) const; + compare(size_type __pos, size_type __n, const basic_string& __str) const + { + _M_check(__pos, "basic_string::compare"); + __n = _M_limit(__pos, __n); + const size_type __osize = __str.size(); + const size_type __len = std::min(__n, __osize); + int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); + if (!__r) + __r = _S_compare(__n, __osize); + return __r; + } /** * @brief Compare substring to a substring. @@ -2879,7 +2889,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ int compare(size_type __pos1, size_type __n1, const basic_string& __str, - size_type __pos2, size_type __n2 = npos) const; + size_type __pos2, size_type __n2 = npos) const + { + _M_check(__pos1, "basic_string::compare"); + __str._M_check(__pos2, "basic_string::compare"); + __n1 = _M_limit(__pos1, __n1); + __n2 = __str._M_limit(__pos2, __n2); + const size_type __len = std::min(__n1, __n2); + int __r = traits_type::compare(_M_data() + __pos1, + __str.data() + __pos2, __len); + if (!__r) + __r = _S_compare(__n1, __n2); + return __r; + } /** * @brief Compare to a C string. @@ -2896,7 +2918,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * ordered first. */ int - compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT; + compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT + { + __glibcxx_requires_string(__s); + const size_type __size = this->size(); + const size_type __osize = traits_type::length(__s); + const size_type __len = std::min(__size, __osize); + int __r = traits_type::compare(_M_data(), __s, __len); + if (!__r) + __r = _S_compare(__size, __osize); + return __r; + } // _GLIBCXX_RESOLVE_LIB_DEFECTS // 5 String::compare specification questionable @@ -2920,7 +2952,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * one is ordered first. */ int - compare(size_type __pos, size_type __n1, const _CharT* __s) const; + compare(size_type __pos, size_type __n1, const _CharT* __s) const + { + __glibcxx_requires_string(__s); + _M_check(__pos, "basic_string::compare"); + __n1 = _M_limit(__pos, __n1); + const size_type __osize = traits_type::length(__s); + const size_type __len = std::min(__n1, __osize); + int __r = traits_type::compare(_M_data() + __pos, __s, __len); + if (!__r) + __r = _S_compare(__n1, __osize); + return __r; + } /** * @brief Compare substring against a character %array. @@ -2948,7 +2991,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ int compare(size_type __pos, size_type __n1, const _CharT* __s, - size_type __n2) const; + size_type __n2) const + { + __glibcxx_requires_string_len(__s, __n2); + _M_check(__pos, "basic_string::compare"); + __n1 = _M_limit(__pos, __n1); + const size_type __len = std::min(__n1, __n2); + int __r = traits_type::compare(_M_data() + __pos, __s, __len); + if (!__r) + __r = _S_compare(__n1, __n2); + return __r; + } #if __cplusplus > 201703L bool diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/compare/char/3.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/compare/char/3.cc new file mode 100644 index 00000000000..8d2917c5145 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/compare/char/3.cc @@ -0,0 +1,7 @@ +// { dg-options "-O1 -g0" } +// { dg-do compile } +// { dg-final { scan-assembler-not "12basic_stringIcSt11char_traitsIcESaIcEE7compare" } } + +#include +bool eq() { return std::string("blah") == "literal"; } +bool lt() { return std::string("blah") < "literal"; }