From: Lancelot SIX <lsix@lancelotsix.com>
To: Tom de Vries <tdevries@suse.de>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH v2 1/4] [gdb/cli] Skip string copy in source_cache::ensure
Date: Fri, 13 Oct 2023 17:34:25 +0100 [thread overview]
Message-ID: <20231013163425.jl53xf5d7c4booy3@octopus> (raw)
In-Reply-To: <20231013121953.25917-2-tdevries@suse.de>
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
>
next prev parent reply other threads:[~2023-10-13 16:34 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-13 12:19 [PATCH v2 0/4] [gdb/cli] Allow source highlighting to be interrupted Tom de Vries
2023-10-13 12:19 ` [PATCH v2 1/4] [gdb/cli] Skip string copy in source_cache::ensure Tom de Vries
2023-10-13 16:34 ` Lancelot SIX [this message]
2023-10-16 7:40 ` Tom de Vries
2023-10-13 12:19 ` [PATCH v2 2/4] [gdb/cli] Factor out try_source_highlight Tom de Vries
2023-10-13 16:52 ` Lancelot SIX
2023-10-16 7:07 ` Tom de Vries
2023-10-16 8:25 ` Lancelot SIX
2023-10-13 12:19 ` [PATCH v2 3/4] [gdb/cli] Keep track of styling failures in source_cache Tom de Vries
2023-10-13 12:19 ` [PATCH v2 4/4] [gdb/cli] Allow source highlighting to be interrupted Tom de Vries
2023-10-13 17:00 ` Lancelot SIX
2023-10-16 7:47 ` Tom de Vries
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231013163425.jl53xf5d7c4booy3@octopus \
--to=lsix@lancelotsix.com \
--cc=gdb-patches@sourceware.org \
--cc=tdevries@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox