From patchwork Sat Jun 1 10:24:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1942411 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=byO5iBSE; 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 4Vrx2p16VYz20Pb for ; Sat, 1 Jun 2024 20:25:38 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 639AC382EF07 for ; Sat, 1 Jun 2024 10:25:36 +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 E0C2A3882672 for ; Sat, 1 Jun 2024 10:25:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E0C2A3882672 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 E0C2A3882672 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=1717237518; cv=none; b=MehbeFMhIIlOTY2cIXklw7twNgVkMI2Z/un2qqb+qHyzw/X/siwD6HMdzvKV68r52c/wSipDxAqLAK4fF+nQm1J14WhQJ4yoha8vCqvJ67QjNQzacRd0hhpmz1I3C6Sikr5dTdPM29e3ZjTRrj65nTqTI17zctcniNPYhbinxlw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717237518; c=relaxed/simple; bh=E8N/9Pv2aF5eU3sXFLoqkxVaPocf/onmkToFZt4T0bI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=dXUnh7YNDYKypzL2yTVKAV7ZYnx2DrLanTqpRO44yrWOa4LPzBovCvEnyWu8LsMS7H/8rKtOm7RXo7RxTrzKa279fKGqbQSzxuqsTrPp80DYRG4YwUoZnAfl0lAidfJ5XYqeXOxUkidDREZ0v0x8oTNsAc5zyudbT0YnNUlu/MQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717237515; 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=QccGCSpPyhT2f2juUy8OTm+ENMMF9kQLQRgQp1ZYKlw=; b=byO5iBSEXQxsOZpOPBV/T0ue9lxadEz1MWGJVtNSJyOAnkfZpBvRMOwqEUJHAFahPJSipr Zdav+mrkPi+98qPZy8towL+rcAaUkXqWLsokQK2wTrNRmH2qK80siJA9QuNnftRk23I0SH N913cwWGWJL9dHeW4rgoSX1j3Fs2dVo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-360-1_roPAbnMG-z-OVgoCgQjA-1; Sat, 01 Jun 2024 06:25:09 -0400 X-MC-Unique: 1_roPAbnMG-z-OVgoCgQjA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id EAFE385A58C; Sat, 1 Jun 2024 10:25:08 +0000 (UTC) Received: from localhost (unknown [10.42.28.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id B8E20C15BB1; Sat, 1 Jun 2024 10:25:08 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Optimize std::basic_string_view::starts_with Date: Sat, 1 Jun 2024 11:24:55 +0100 Message-ID: <20240601102508.878398-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.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 We get smaller code at all optimization levels by not creating a temporary object, just comparing lengths first and then using traits_type::compare. This does less work than calling substr then operator==. libstdc++-v3/ChangeLog: * include/std/string_view (starts_with(basic_string_view)): Compare lengths first and then call traits_type::compare directly. --- libstdc++-v3/include/std/string_view | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index a7c5a126461..740aa9344f0 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -385,7 +385,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION [[nodiscard]] constexpr bool starts_with(basic_string_view __x) const noexcept - { return this->substr(0, __x.size()) == __x; } + { + return _M_len >= __x._M_len + && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0; + } [[nodiscard]] constexpr bool