]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fast-export: stop storing lengths in anonymized hashmaps
authorJeff King <peff@peff.net>
Tue, 23 Jun 2020 15:24:56 +0000 (11:24 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jun 2020 02:56:26 +0000 (19:56 -0700)
Now that the anonymize_str() interface is restricted to NUL-terminated
strings, there's no need for us to keep track of the length of each
entry in the hashmap. This simplifies the code and saves a bit of
memory.

Note that we do still need to compare the stored results to partial
strings passed in by the callers. We can do that by using hashmap's
keydata feature to get the ptr/len pair into the comparison function,
and then using strncmp().

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fast-export.c

index d8ea067630f94600dd86a83f649aef652d33c07a..5df2ada47d03a865b8279c14cc860d7f240262c6 100644 (file)
@@ -121,23 +121,32 @@ static int has_unshown_parent(struct commit *commit)
 struct anonymized_entry {
        struct hashmap_entry hash;
        const char *orig;
-       size_t orig_len;
        const char *anon;
-       size_t anon_len;
+};
+
+struct anonymized_entry_key {
+       struct hashmap_entry hash;
+       const char *orig;
+       size_t orig_len;
 };
 
 static int anonymized_entry_cmp(const void *unused_cmp_data,
                                const struct hashmap_entry *eptr,
                                const struct hashmap_entry *entry_or_key,
-                               const void *unused_keydata)
+                               const void *keydata)
 {
        const struct anonymized_entry *a, *b;
 
        a = container_of(eptr, const struct anonymized_entry, hash);
-       b = container_of(entry_or_key, const struct anonymized_entry, hash);
+       if (keydata) {
+               const struct anonymized_entry_key *key = keydata;
+               int equal = !strncmp(a->orig, key->orig, key->orig_len) &&
+                           !a->orig[key->orig_len];
+               return !equal;
+       }
 
-       return a->orig_len != b->orig_len ||
-               memcmp(a->orig, b->orig, a->orig_len);
+       b = container_of(entry_or_key, const struct anonymized_entry, hash);
+       return strcmp(a->orig, b->orig);
 }
 
 /*
@@ -149,7 +158,8 @@ static const char *anonymize_str(struct hashmap *map,
                                 char *(*generate)(const char *, size_t),
                                 const char *orig, size_t len)
 {
-       struct anonymized_entry key, *ret;
+       struct anonymized_entry_key key;
+       struct anonymized_entry *ret;
 
        if (!map->cmpfn)
                hashmap_init(map, anonymized_entry_cmp, NULL, 0);
@@ -157,15 +167,13 @@ static const char *anonymize_str(struct hashmap *map,
        hashmap_entry_init(&key.hash, memhash(orig, len));
        key.orig = orig;
        key.orig_len = len;
-       ret = hashmap_get_entry(map, &key, hash, NULL);
+       ret = hashmap_get_entry(map, &key, hash, &key);
 
        if (!ret) {
                ret = xmalloc(sizeof(*ret));
                hashmap_entry_init(&ret->hash, key.hash.hash);
                ret->orig = xmemdupz(orig, len);
-               ret->orig_len = len;
                ret->anon = generate(orig, len);
-               ret->anon_len = strlen(ret->anon);
                hashmap_put(map, &ret->hash);
        }