Skip to content

Commit d6484e9

Browse files
peffgitster
authored andcommitted
fast-export: simplify initialization of anonymized hashmaps
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 b7879b0 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 76e50f7 commit d6484e9

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

builtin/fast-export.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,22 @@ static const char *anonymize_str(struct hashmap *map,
152152
struct anonymized_entry_key key;
153153
struct anonymized_entry *ret;
154154

155-
if (!map->cmpfn)
156-
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
157-
158155
hashmap_entry_init(&key.hash, memhash(orig, len));
159156
key.orig = orig;
160157
key.orig_len = len;
161158

162159
/* First check if it's a token the user configured manually... */
163-
if (anonymized_seeds.cmpfn)
164-
ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
165-
else
166-
ret = NULL;
160+
ret = hashmap_get_entry(&anonymized_seeds, &key, hash, &key);
167161

168162
/* ...otherwise check if we've already seen it in this context... */
169163
if (!ret)
170164
ret = hashmap_get_entry(map, &key, hash, &key);
171165

172166
/* ...and finally generate a new mapping if necessary */
173167
if (!ret) {
168+
if (!map->cmpfn)
169+
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
170+
174171
FLEX_ALLOC_MEM(ret, orig, orig, len);
175172
hashmap_entry_init(&ret->hash, key.hash.hash);
176173
ret->anon = generate(data);

0 commit comments

Comments
 (0)