@matheusbsbs / matheus-bagliano / commits / a0f65641df
Fork 0

fast-export: stop storing lengths in anonymized hashmaps

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>

Jeff King committed Jun 23, 2020 at 11:24 UTC a0f65641dfb67ffa94f1e33ded2cbab8d77d53a5
1 file changed +18 -10
builtin/fast-export.c
+18 -10
@@ -121,23 +121,32 @@ static int has_unshown_parent(struct commit *commit)
121 struct anonymized_entry {
122 struct hashmap_entry hash;
123 const char *orig;
124 - size_t orig_len;
124 const char *anon;
126 - size_t anon_len;
125 +};
126 +
127 +struct anonymized_entry_key {
128 + struct hashmap_entry hash;
129 + const char *orig;
130 + size_t orig_len;
131 };
132
133 static int anonymized_entry_cmp(const void *unused_cmp_data,
134 const struct hashmap_entry *eptr,
135 const struct hashmap_entry *entry_or_key,
132 - const void *unused_keydata)
136 + const void *keydata)
137 {
138 const struct anonymized_entry *a, *b;
139
140 a = container_of(eptr, const struct anonymized_entry, hash);
137 - b = container_of(entry_or_key, const struct anonymized_entry, hash);
141 + if (keydata) {
142 + const struct anonymized_entry_key *key = keydata;
143 + int equal = !strncmp(a->orig, key->orig, key->orig_len) &&
144 + !a->orig[key->orig_len];
145 + return !equal;
146 + }
147
139 - return a->orig_len != b->orig_len ||
140 - memcmp(a->orig, b->orig, a->orig_len);
148 + b = container_of(entry_or_key, const struct anonymized_entry, hash);
149 + return strcmp(a->orig, b->orig);
150 }
151
152 /*
@@ -149,7 +158,8 @@ static const char *anonymize_str(struct hashmap *map,
158 char *(*generate)(const char *, size_t),
159 const char *orig, size_t len)
160 {
152 - struct anonymized_entry key, *ret;
161 + struct anonymized_entry_key key;
162 + struct anonymized_entry *ret;
163
164 if (!map->cmpfn)
165 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
@@ -157,15 +167,13 @@ static const char *anonymize_str(struct hashmap *map,
167 hashmap_entry_init(&key.hash, memhash(orig, len));
168 key.orig = orig;
169 key.orig_len = len;
160 - ret = hashmap_get_entry(map, &key, hash, NULL);
170 + ret = hashmap_get_entry(map, &key, hash, &key);
171
172 if (!ret) {
173 ret = xmalloc(sizeof(*ret));
174 hashmap_entry_init(&ret->hash, key.hash.hash);
175 ret->orig = xmemdupz(orig, len);
166 - ret->orig_len = len;
176 ret->anon = generate(orig, len);
168 - ret->anon_len = strlen(ret->anon);
177 hashmap_put(map, &ret->hash);
178 }
179