diff mbox series

[1/4] abg-writer: get_id_for_type: refactor insertion and look-up

Message ID 20210910112325.806676-2-gprocida@google.com
State New
Headers show
Series Looking at equality and hashing | expand

Commit Message

Giuliano Procida Sept. 10, 2021, 11:23 a.m. UTC
The insertion and look-up in get_id_for_type can be refactored. This
avoids an extra round of hashing and equality tests in the case of a
newly inserted type.

	* src/abg-writer.cc (get_id_for_type): Fold together the
	look-up and insertion logic by always attempting to insert a
	dummy value.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-writer.cc | 47 ++++++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 21 deletions(-)
diff mbox series

Patch

diff --git a/src/abg-writer.cc b/src/abg-writer.cc
index 9f48dc92..d23f4fd6 100644
--- a/src/abg-writer.cc
+++ b/src/abg-writer.cc
@@ -421,30 +421,35 @@  public:
     if (c == 0)
       c = const_cast<type_base*>(t);
 
-    type_ptr_map::const_iterator it = m_type_id_map.find(c);
-    if (it != m_type_id_map.end())
-      return it->second;
+    auto insertion = m_type_id_map.emplace(c, interned_string());
+    interned_string& id = insertion.first->second;
 
-    switch (m_type_id_style)
-      {
-      case SEQUENCE_TYPE_ID_STYLE:
-	{
-	  interned_string id = get_id_manager().get_id_with_prefix("type-id-");
-	  return m_type_id_map[c] = id;
-	}
-      case HASH_TYPE_ID_STYLE:
+    if (insertion.second)
+      switch (m_type_id_style)
 	{
-	  interned_string pretty = c->get_cached_pretty_representation(true);
-	  size_t hash = hashing::fnv_hash(pretty);
-	  while (!m_used_type_id_hashes.insert(hash).second)
-	    ++hash;
-	  std::ostringstream os;
-	  os << std::hex << std::setfill('0') << std::setw(8) << hash;
-	  return m_type_id_map[c] = c->get_environment()->intern(os.str());
+	case SEQUENCE_TYPE_ID_STYLE:
+	  {
+	    id = get_id_manager().get_id_with_prefix("type-id-");
+	    break;
+	  }
+	case HASH_TYPE_ID_STYLE:
+	  {
+	    interned_string pretty = c->get_cached_pretty_representation(true);
+	    size_t hash = hashing::fnv_hash(pretty);
+	    while (!m_used_type_id_hashes.insert(hash).second)
+	      ++hash;
+	    std::ostringstream os;
+	    os << std::hex << std::setfill('0') << std::setw(8) << hash;
+	    id = c->get_environment()->intern(os.str());
+	    break;
+	  }
+	  default: {
+	    ABG_ASSERT_NOT_REACHED;
+	    break;
+	  }
 	}
-      }
-    ABG_ASSERT_NOT_REACHED;
-    return interned_string();
+
+    return id;
   }
 
   string