From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id EqIdB6txKWXr1SwAWB0awg (envelope-from ) for ; Fri, 13 Oct 2023 12:34:51 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=lancelotsix.com header.i=@lancelotsix.com header.a=rsa-sha256 header.s=2021 header.b=wi4hKhI/; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 0E96F1E0C1; Fri, 13 Oct 2023 12:34:51 -0400 (EDT) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id F16B31E091 for ; Fri, 13 Oct 2023 12:34:48 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 045813857728 for ; Fri, 13 Oct 2023 16:34:48 +0000 (GMT) Received: from lndn.lancelotsix.com (lndn.lancelotsix.com [51.195.220.111]) by sourceware.org (Postfix) with ESMTPS id B0A423858D1E for ; Fri, 13 Oct 2023 16:34:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B0A423858D1E Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=lancelotsix.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=lancelotsix.com Received: from octopus (cust120-dsl54.idnet.net [212.69.54.120]) by lndn.lancelotsix.com (Postfix) with ESMTPSA id 2D179864F6; Fri, 13 Oct 2023 16:34:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lancelotsix.com; s=2021; t=1697214870; bh=rhbnjVT3P6txlsPpcOPI8GFODOEenkvv7JS7GnmNWTk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=wi4hKhI/dkhFJcrRIFrbmpsOyXIvdUPXSoruKhqjBi575w9xxYrPVxn2iPSUvHmgW jql107Xt0nXWLaDCxqU3G56em+sUSR4BfIrODo2zRTKYYR6APDeq7zyLvhtD6OXq3A 2uQ+TZkxGRZH6ZHPhpU+jEHIeX0i87Epx0MjHqmaCQnfkZP1Mh0i7sYIjEE9P8PGD1 xlqMHJTpjifCrQArf4+eVSF/sgH7cKeX2tZrLQOlmu0zlkeiIXY2urDXuWjqTz53rb Ot71GPWyMWn4SOorABTraNWTE4LLXC39FHXTLca/irf0oCUUU17CaD0/uoOZiiHBUc W3It8HrDg4Svw== Date: Fri, 13 Oct 2023 17:34:25 +0100 From: Lancelot SIX To: Tom de Vries Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v2 1/4] [gdb/cli] Skip string copy in source_cache::ensure Message-ID: <20231013163425.jl53xf5d7c4booy3@octopus> References: <20231013121953.25917-1-tdevries@suse.de> <20231013121953.25917-2-tdevries@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231013121953.25917-2-tdevries@suse.de> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.2 (lndn.lancelotsix.com [0.0.0.0]); Fri, 13 Oct 2023 16:34:30 +0000 (UTC) X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Hi Tom, On Fri, Oct 13, 2023 at 02:19:50PM +0200, Tom de Vries wrote: > In function source_cache::ensure we have: > ... > std::ostringstream output; > ... > contents = output.str(); > ... > The last line causes an unnecessary string copy. > > C++20 allows us to skip it, like this: > ... > contents = std::move (output).str(); > ... Looking at cppreference[1], it suggests that this new C++ overload for str should be equivalent to content = std::move (*output.rbuf ()).str (); Should we use this construct instead regardless of C++'s version and avoid the #ifdef? That being said, I find the C++20 version easier to read, so it's up to you. Best, Lancelot. [1] https://en.cppreference.com/w/cpp/io/basic_ostringstream/str > > Use the more efficient version if compiling with -std=c++20. > > Tested on x86_64-linux. > --- > gdb/source-cache.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/gdb/source-cache.c b/gdb/source-cache.c > index 77b357cb42b..7ef54411c69 100644 > --- a/gdb/source-cache.c > +++ b/gdb/source-cache.c > @@ -252,7 +252,11 @@ source_cache::ensure (struct symtab *s) > std::istringstream input (contents); > std::ostringstream output; > highlighter->highlight (input, output, lang_name, fullname); > +#if __cplusplus >= 202002L > + contents = std::move (output).str(); > +#else > contents = output.str (); > +#endif > already_styled = true; > } > catch (...) > -- > 2.35.3 >