[v2,4/8] Use try_emplace in index-write.c

Message ID 20231121152817.31859-5-tom@tromey.com
State New
Headers
Series [v2,1/8] Use C++17 [[fallthrough]] attribute |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom Tromey Nov. 21, 2023, 3:27 p.m. UTC
  index-write.c has a comment indicating that C++17's try_emplace could
be used.  This patch makes the change.

Approved-By: Pedro Alves <pedro@palves.net>
---
 gdb/dwarf2/index-write.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
  

Patch

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index d1b10a28823..3aafc1aab74 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -385,24 +385,17 @@  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 ())
+	auto [iter, inserted]
+	  = symbol_hash_table.try_emplace (entry.cu_indices,
+					   cpool.size ());
+	entry.index_offset = iter->second;
+	if (inserted)
 	  {
-	    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);
       }
   }