public inbox for libabigail@sourceware.org
 help / color / mirror / Atom feed
From: Giuliano Procida <gprocida@google.com>
To: libabigail@sourceware.org
Cc: dodji@seketeli.org, kernel-team@android.com, gprocida@google.com,
	 maennich@google.com
Subject: [PATCH 1/4] abg-writer: get_id_for_type: refactor insertion and look-up
Date: Fri, 10 Sep 2021 12:23:22 +0100	[thread overview]
Message-ID: <20210910112325.806676-2-gprocida@google.com> (raw)
In-Reply-To: <20210910112325.806676-1-gprocida@google.com>

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 --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
-- 
2.33.0.309.g3052b89438-goog


  reply	other threads:[~2021-09-10 11:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-10 11:23 [PATCH 0/4] Looking at equality and hashing Giuliano Procida
2021-09-10 11:23 ` Giuliano Procida [this message]
2021-09-10 11:23 ` [PATCH 2/4] abg-writer: refactor recording of unemitted types Giuliano Procida
2021-09-10 11:23 ` [PATCH 3/4] abg-writer: allow debugging of equality / hash issues Giuliano Procida
2021-09-10 11:23 ` [PATCH 4/4] RFC: abg-writer: add a missing check for emitted declarations Giuliano Procida

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=20210910112325.806676-2-gprocida@google.com \
    --to=gprocida@google.com \
    --cc=dodji@seketeli.org \
    --cc=kernel-team@android.com \
    --cc=libabigail@sourceware.org \
    --cc=maennich@google.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).