convert hashmap comparison functions to oideq()

The comparison functions used for hashmaps don't care about strict ordering; they only want to compare entries for equality. Let's use the oideq() function instead, which can potentially be better optimized. Note that unlike the previous patches mass-converting calls like "!oidcmp()", this patch could actually provide an improvement even with the current implementation. Those comparison functions are passed around as function pointers, so at compile-time the compiler cannot realize that the caller (which is in another file completely) will treat the return value as a boolean. Note that this does change the return values in quite a subtle way (it's still an int, but now the sign bit is irrelevant for ordering). Because of their funny hashmap-specific signature, it's unlikely that any of these static functions would be reused for more generic ordering. But to be double-sure, let's stop using "cmp" in their names. Calling them "eq" doesn't quite work either, because the hashmap convention is actually _inverted_. "0" means "same", and non-zero means "different". So I've called them "neq" by convention here. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 28, 2018 at 17:22 UTC cc00e5ce6b50cf4197e137b87a33209568b1e662
3 files changed +13 -13
builtin/describe.c
+3 -3
@@ -62,7 +62,7 @@ static const char *prio_names[] = {
62 N_("head"), N_("lightweight"), N_("annotated"),
63 };
64
65 -static int commit_name_cmp(const void *unused_cmp_data,
65 +static int commit_name_neq(const void *unused_cmp_data,
66 const void *entry,
67 const void *entry_or_key,
68 const void *peeled)
@@ -70,7 +70,7 @@ static int commit_name_cmp(const void *unused_cmp_data,
70 const struct commit_name *cn1 = entry;
71 const struct commit_name *cn2 = entry_or_key;
72
73 - return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
73 + return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled);
74 }
75
76 static inline struct commit_name *find_commit_name(const struct object_id *peeled)
@@ -596,7 +596,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
596 return cmd_name_rev(args.argc, args.argv, prefix);
597 }
598
599 - hashmap_init(&names, commit_name_cmp, NULL, 0);
599 + hashmap_init(&names, commit_name_neq, NULL, 0);
600 for_each_rawref(get_name, NULL);
601 if (!hashmap_get_size(&names) && !always)
602 die(_("No names found, cannot describe anything."));
oidmap.c
+6 -6
@@ -1,14 +1,14 @@
1 #include "cache.h"
2 #include "oidmap.h"
3
4 -static int cmpfn(const void *hashmap_cmp_fn_data,
5 - const void *entry, const void *entry_or_key,
6 - const void *keydata)
4 +static int oidmap_neq(const void *hashmap_cmp_fn_data,
5 + const void *entry, const void *entry_or_key,
6 + const void *keydata)
7 {
8 const struct oidmap_entry *entry_ = entry;
9 if (keydata)
10 - return oidcmp(&entry_->oid, (const struct object_id *) keydata);
11 - return oidcmp(&entry_->oid,
10 + return !oideq(&entry_->oid, (const struct object_id *) keydata);
11 + return !oideq(&entry_->oid,
12 &((const struct oidmap_entry *) entry_or_key)->oid);
13 }
14
@@ -21,7 +21,7 @@ static int hash(const struct object_id *oid)
21
22 void oidmap_init(struct oidmap *map, size_t initial_size)
23 {
24 - hashmap_init(&map->map, cmpfn, NULL, initial_size);
24 + hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
25 }
26
27 void oidmap_free(struct oidmap *map, int free_entries)
patch-ids.c
+4 -4
@@ -28,14 +28,14 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
28 /*
29 * When we cannot load the full patch-id for both commits for whatever
30 * reason, the function returns -1 (i.e. return error(...)). Despite
31 - * the "cmp" in the name of this function, the caller only cares about
31 + * the "neq" in the name of this function, the caller only cares about
32 * the return value being zero (a and b are equivalent) or non-zero (a
33 * and b are different), and returning non-zero would keep both in the
34 * result, even if they actually were equivalent, in order to err on
35 * the side of safety. The actual value being negative does not have
36 * any significance; only that it is non-zero matters.
37 */
38 -static int patch_id_cmp(const void *cmpfn_data,
38 +static int patch_id_neq(const void *cmpfn_data,
39 const void *entry,
40 const void *entry_or_key,
41 const void *unused_keydata)
@@ -53,7 +53,7 @@ static int patch_id_cmp(const void *cmpfn_data,
53 commit_patch_id(b->commit, opt, &b->patch_id, 0))
54 return error("Could not get patch ID for %s",
55 oid_to_hex(&b->commit->object.oid));
56 - return oidcmp(&a->patch_id, &b->patch_id);
56 + return !oideq(&a->patch_id, &b->patch_id);
57 }
58
59 int init_patch_ids(struct patch_ids *ids)
@@ -63,7 +63,7 @@ int init_patch_ids(struct patch_ids *ids)
63 ids->diffopts.detect_rename = 0;
64 ids->diffopts.flags.recursive = 1;
65 diff_setup_done(&ids->diffopts);
66 - hashmap_init(&ids->patches, patch_id_cmp, &ids->diffopts, 256);
66 + hashmap_init(&ids->patches, patch_id_neq, &ids->diffopts, 256);
67 return 0;
68 }
69