From patchwork Sat Dec 16 20:10:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antony Polukhin X-Patchwork-Id: 1876986 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=ane/r2dB; 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 4Ssxzr18sWz23ns for ; Sun, 17 Dec 2023 07:11:02 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 463533858D38 for ; Sat, 16 Dec 2023 20:10:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id 012A03858D28; Sat, 16 Dec 2023 20:10:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 012A03858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 012A03858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::32c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702757447; cv=none; b=alaw0HpgHPRsAOwHHEdgIFptZbFcoYPrEDyZwbsCm4aefFFUeEuVDxHJFjzLRx3ZC3ffBp3+2soW+2jPJI0Svat2C0PomUlwtzB57OGIZQBqAMVz73xKDumUCzw0s87SMfHug1hHYsYCK7Qqf7BnmXsvL8wmZXNoISRYFTHPbEc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702757447; c=relaxed/simple; bh=madTjnM8bCftpYxU4dU6lWoeM1z53h9ylqc62GZlZ28=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=JkUME3tWPX17y76McsZLEVP2LBMjD7/N739gRSyNsidP49N43xdcEZUt5zp9g3Rc3of2pxK3Z+XFGvIy16xIfO4O4ZWnUXJ+dYyT9IqQe4JHOZdS6QS5P+8UuZRezr3CYcjum/mnqX71UC3V5C857m2qWMlGF5L2ds7dVI+qwQw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-6d9e0f0cba9so1435858a34.1; Sat, 16 Dec 2023 12:10:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702757445; x=1703362245; darn=gcc.gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=QPHmMQEPtAH6CC1gSE2OG5QsriYJl9qquDOzeZr2RnM=; b=ane/r2dBsNzKco6ZStByjorcJWo8MpZDp/Ud4RmRW4n0pn4WQuue1bfv2IqOaS4sci pQQJ49UBMfCiiI9w2Vm0vCjyE30Rgflgvqer0Efya8JLbIPLuLgZ2zNn/niulpyip4Kl g8au43LyOdGFnupiQvbKdyEfGJilLUcnzF701k0i4yW2TcH46Oj2mTA3N/QePITVzunI rJUz61Y1XC6lCHkdVuQKUCR/AseY9xi/gZ/0mxyVUwf7cki7iZn9Abrv5buG0yTIvDUX nwvf8AQ3t/IcBxK2pavmH4os6BRmicLSzERU5UcCPV8f1H5qUyReklWRilWwmpnYJ48V Nd2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702757445; x=1703362245; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QPHmMQEPtAH6CC1gSE2OG5QsriYJl9qquDOzeZr2RnM=; b=sjF9s4CZzCqsRPggArMl95OQdwYAY7hKuDYHmgf37NrWkhdGWz2CvtrsYQxhA0nois hdgGFgXYswxNhYurpcqLS0FcAnyqWDy5nL9Y930c8RWlK+jF5HM1lZpt47wfBpGUtyhT R2cvQOZul0Ml0CYkwrn2BpTFH9vs4T3kFIpWB/w9C4aYDwbyceOSAzJAEyA4o3u1ny5Z SplaYSXKtNqnGtsyKM5cotPUdAQVxUS2Ikp5tDm3xEdnYE3xGpthHZ8j8o6sdGSjDElC NKw0TGtuqJUU2DzSVtiGCWjF3nP2bceI9HtzaXKFyuDoExwYAIscL7d0iDhYFaIwr2oL 29ag== X-Gm-Message-State: AOJu0YyP36yI3XtkzVRS3tpKhv086f+RX6cvb+R0FDoBIWxbDN0wlEdh 1U15hshbuaDiWGcx0M0fjVQLQK0FsYCgfCcMrtW2l/1kJnc= X-Google-Smtp-Source: AGHT+IFNfx30nnlWkFPfS0L9Nuv+qDEBrlCUuf/xhZY6w/+SIlLXZ/yN+sbPcKTslEcSskelCrENjt3zdQ9vgPH1y9c= X-Received: by 2002:a05:6808:38c3:b0:3ba:5dd:9457 with SMTP id el3-20020a05680838c300b003ba05dd9457mr19688400oib.38.1702757444849; Sat, 16 Dec 2023 12:10:44 -0800 (PST) MIME-Version: 1.0 From: Antony Polukhin Date: Sat, 16 Dec 2023 23:10:33 +0300 Message-ID: Subject: [PATCH] PR libstdc++/112682 More efficient std::basic_string move To: "libstdc++" , gcc-patches List X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.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 A few places in bits/basic_string.h use `traits_type::copy` to copy `__str.length() + 1` bytes. Despite the knowledge that `__str.length()` is not greater than 15 the compiler emits (and sometimes inlines) a `memcpy` call. That results in a quite big set of instructions https://godbolt.org/z/j35MMfxzq Replacing `__str.length() + 1` with `_S_local_capacity + 1` explicitly forces the compiler to copy the whole `__str._M_local_buf`. As a result the assembly becomes almost 5 times shorter and without any function calls or multiple conditional jumps https://godbolt.org/z/bfq8bxra9 This patch always copies `_S_local_capacity + 1` if working with `std::char_traits`. PR libstdc++/112682: * include/bits/basic_string.h: Optimize string moves. P.S.: still not sure that this optimization is not an UB or fine for libstdc++. However, the assembly looks much better with it. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 1b8ebca7dad..7a5e348280c 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -188,6 +188,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { } #endif + _GLIBCXX17_CONSTEXPR + static bool + _S_permit_copying_indeterminate() noexcept + { + // Copying compile-time known _S_local_capacity + 1 bytes is much more + // efficient than copying runtime known __str.length() + 1. This + // function returns true, if such initialization is permitted even if + // the right side has indeterminate values. + // + // [dcl.init] permits initializing with indeterminate value of unsigned + // narrow character type. + // + // Library users should not specialize char_traits so this is + // not observable for user. + return is_same >::value; + } + // Use empty-base optimization: http://www.cantrip.org/emptyopt.html struct _Alloc_hider : allocator_type // TODO check __is_final { @@ -672,8 +689,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { if (__str._M_is_local()) { - traits_type::copy(_M_local_buf, __str._M_local_buf, - __str.length() + 1); + size_type __copy_count = _S_local_capacity + 1; + if _GLIBCXX17_CONSTEXPR (!_S_permit_copying_indeterminate()) + __copy_count = __str.length() + 1; + traits_type::copy(_M_local_buf, __str._M_local_buf, __copy_count); } else { @@ -711,8 +730,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { if (__str._M_is_local()) { - traits_type::copy(_M_local_buf, __str._M_local_buf, - __str.length() + 1); + size_type __copy_count = _S_local_capacity + 1; + if _GLIBCXX17_CONSTEXPR (!_S_permit_copying_indeterminate()) + __copy_count = __str.length() + 1; + traits_type::copy(_M_local_buf, __str._M_local_buf, __copy_count); _M_length(__str.length()); __str._M_set_length(0); }