Skip to content

Commit a0f6564

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7f40759 commit a0f6564

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

builtin/fast-export.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,32 @@ static int has_unshown_parent(struct commit *commit)
121121
struct anonymized_entry {
122122
struct hashmap_entry hash;
123123
const char *orig;
124-
size_t orig_len;
125124
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;
127131
};
128132

129133
static int anonymized_entry_cmp(const void *unused_cmp_data,
130134
const struct hashmap_entry *eptr,
131135
const struct hashmap_entry *entry_or_key,
132-
const void *unused_keydata)
136+
const void *keydata)
133137
{
134138
const struct anonymized_entry *a, *b;
135139

136140
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+
}
138147

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);
141150
}
142151

143152
/*
@@ -149,23 +158,22 @@ static const char *anonymize_str(struct hashmap *map,
149158
char *(*generate)(const char *, size_t),
150159
const char *orig, size_t len)
151160
{
152-
struct anonymized_entry key, *ret;
161+
struct anonymized_entry_key key;
162+
struct anonymized_entry *ret;
153163

154164
if (!map->cmpfn)
155165
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
156166

157167
hashmap_entry_init(&key.hash, memhash(orig, len));
158168
key.orig = orig;
159169
key.orig_len = len;
160-
ret = hashmap_get_entry(map, &key, hash, NULL);
170+
ret = hashmap_get_entry(map, &key, hash, &key);
161171

162172
if (!ret) {
163173
ret = xmalloc(sizeof(*ret));
164174
hashmap_entry_init(&ret->hash, key.hash.hash);
165175
ret->orig = xmemdupz(orig, len);
166-
ret->orig_len = len;
167176
ret->anon = generate(orig, len);
168-
ret->anon_len = strlen(ret->anon);
169177
hashmap_put(map, &ret->hash);
170178
}
171179

0 commit comments

Comments
 (0)