git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 2/6] fast-export: simplify initialization of anonymized hashmaps
Date: Wed, 22 Mar 2023 13:38:04 -0400	[thread overview]
Message-ID: <20230322173804.GB5795@coredump.intra.peff.net> (raw)
In-Reply-To: <20230322173636.GA5643@coredump.intra.peff.net>

We take pains to avoid doing a lookup on a hashmap which has not been
initialized with hashmap_init(). That was necessary back when this code
was written. But hashmap_get() became safer in b7879b0ba6e (hashmap:
allow re-use after hashmap_free(), 2020-11-02). Since then it's OK to
call functions on a zero-initialized table; it will just correctly
return NULL, since there is no match.

This simplifies the code a little, and also lets us keep the
initialization line closer to when we add an entry (which is when the
hashmap really does need to be totally initialized). That will help
later refactoring.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/fast-export.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index f422819c82a..ba9ab3a97e5 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -152,25 +152,22 @@ static const char *anonymize_str(struct hashmap *map,
 	struct anonymized_entry_key key;
 	struct anonymized_entry *ret;
 
-	if (!map->cmpfn)
-		hashmap_init(map, anonymized_entry_cmp, NULL, 0);
-
 	hashmap_entry_init(&key.hash, memhash(orig, len));
 	key.orig = orig;
 	key.orig_len = len;
 
 	/* First check if it's a token the user configured manually... */
-	if (anonymized_seeds.cmpfn)
-		ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
-	else
-		ret = NULL;
+	ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
 
 	/* ...otherwise check if we've already seen it in this context... */
 	if (!ret)
 		ret = hashmap_get_entry(map, &key, hash, &key);
 
 	/* ...and finally generate a new mapping if necessary */
 	if (!ret) {
+		if (!map->cmpfn)
+			hashmap_init(map, anonymized_entry_cmp, NULL, 0);
+
 		FLEX_ALLOC_MEM(ret, orig, orig, len);
 		hashmap_entry_init(&ret->hash, key.hash.hash);
 		ret->anon = generate(data);
-- 
2.40.0.595.g9b96b494d8c


  parent reply	other threads:[~2023-03-22 17:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22 17:36 [PATCH 0/6] minor cleanups to fast-export --anonymize Jeff King
2023-03-22 17:37 ` [PATCH 1/6] fast-export: drop const when storing anonymized values Jeff King
2023-03-22 17:38 ` Jeff King [this message]
2023-03-22 17:40 ` [PATCH 3/6] fast-export: factor out anonymized_entry creation Jeff King
2023-03-22 17:42 ` [PATCH 4/6] fast-export: de-obfuscate --anonymize-map handling Jeff King
2023-03-22 17:42 ` [PATCH 5/6] fast-export: drop data parameter from anonymous generators Jeff King
2023-03-22 17:43 ` [PATCH 6/6] fast-export: drop unused parameter from anonymize_commit_message() Jeff King
2023-03-24 17:40 ` [PATCH 0/6] minor cleanups to fast-export --anonymize Derrick Stolee
2023-03-24 19:46   ` Junio C Hamano

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230322173804.GB5795@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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).