Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: Tom Tromey <tom@tromey.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 4/7] Use try_emplace in index-write.c
Date: Tue, 17 Oct 2023 09:56:20 +0100	[thread overview]
Message-ID: <74ae9885-03ef-45f2-901e-13dd7a69de85@palves.net> (raw)
In-Reply-To: <20231016230527.1820819-5-tom@tromey.com>

On 2023-10-17 00:02, Tom Tromey wrote:
> index-write.c has a comment indicating that C++17's try_emplace could
> be used.  This patch makes the change.
> ---
>  gdb/dwarf2/index-write.c | 24 ++++++++----------------
>  1 file changed, 8 insertions(+), 16 deletions(-)
> 
> diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
> index a6d770c9ee5..e402d407ee7 100644
> --- a/gdb/dwarf2/index-write.c
> +++ b/gdb/dwarf2/index-write.c
> @@ -384,24 +384,16 @@ write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
>  	  continue;
>  	gdb_assert (entry.index_offset == 0);
>  
> -	/* Finding before inserting is faster than always trying to
> -	   insert, because inserting always allocates a node, does the
> -	   lookup, and then destroys the new node if another node
> -	   already had the same key.  C++17 try_emplace will avoid
> -	   this.  */
> -	const auto found
> -	  = symbol_hash_table.find (entry.cu_indices);
> -	if (found != symbol_hash_table.end ())
> +	const auto &pair = symbol_hash_table.try_emplace (entry.cu_indices,
> +							  cpool.size ());

A little odd to take a const ref to the returned pair instead of taking
it by value.  Better yet, C++17 gives us structured bindings, which lets us
hide the ugly pair completely and use helpful names instead of first/second:

  auto [it, ins] = symbol_hash_table.try_emplace (entry.cu_indices, 
                                                  cpool.size ());

How about we use it here?

Pedro Alves

> +	entry.index_offset = pair.first->second;
> +	if (pair.second)
>  	  {
> -	    entry.index_offset = found->second;
> -	    continue;
> +	    /* Newly inserted.  */
> +	    cpool.append_offset (entry.cu_indices.size ());
> +	    for (const auto index : entry.cu_indices)
> +	      cpool.append_offset (index);
>  	  }
> -
> -	symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
> -	entry.index_offset = cpool.size ();
> -	cpool.append_offset (entry.cu_indices.size ());
> -	for (const auto index : entry.cu_indices)
> -	  cpool.append_offset (index);
>        }
>    }
>  


  reply	other threads:[~2023-10-17  8:56 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-16 23:02 [PATCH 0/7] More C++17 Updates Tom Tromey
2023-10-16 23:02 ` [PATCH 1/7] Use C++17 [[fallthrough]] attribute Tom Tromey
2023-10-16 23:17   ` John Baldwin
2023-10-19 20:23     ` Tom Tromey
2023-10-17  8:53   ` Luis Machado
2023-10-19 20:23     ` Tom Tromey
2023-10-16 23:02 ` [PATCH 2/7] Switch to -Wimplicit-fallthrough=5 Tom Tromey
2023-10-16 23:02 ` [PATCH 3/7] Enable some C++14 code in array-view.h Tom Tromey
2023-10-16 23:02 ` [PATCH 4/7] Use try_emplace in index-write.c Tom Tromey
2023-10-17  8:56   ` Pedro Alves [this message]
2023-10-17  9:30     ` Lancelot SIX
2023-10-19 20:24     ` Tom Tromey
2023-10-16 23:02 ` [PATCH 5/7] Rely on C++17 <new> in new-op.cc Tom Tromey
2023-10-16 23:02 ` [PATCH 6/7] Rely on copy elision in scope-exit.h Tom Tromey
2023-10-16 23:02 ` [PATCH 7/7] Use C++17 void_t Tom Tromey
2023-10-17  9:06 ` [PATCH 0/7] More C++17 Updates Pedro Alves
2023-10-19 20:26   ` Tom Tromey

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=74ae9885-03ef-45f2-901e-13dd7a69de85@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /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