Skip to content

Commit 3cfa4db

Browse files
anderskgitster
authored andcommitted
describe: Store commit_names in a hash table by commit SHA1
describe is currently forced to look up the commit at each tag in order to store the struct commit_name pointers in struct commit.util. For --exact-match queries, those lookups are wasteful. In preparation for removing them, put the commit_names into a hash table, indexed by commit SHA1, that can be used to quickly check for exact matches. Signed-off-by: Anders Kaseorg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e1ade1 commit 3cfa4db

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

builtin/describe.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "exec_cmd.h"
77
#include "parse-options.h"
88
#include "diff.h"
9+
#include "hash.h"
910

1011
#define SEEN (1u<<0)
1112
#define MAX_TAGS (FLAG_BITS - 1)
@@ -22,7 +23,7 @@ static int tags; /* Allow lightweight tags */
2223
static int longformat;
2324
static int abbrev = DEFAULT_ABBREV;
2425
static int max_candidates = 10;
25-
static int found_names;
26+
static struct hash_table names;
2627
static const char *pattern;
2728
static int always;
2829
static const char *dirty;
@@ -34,6 +35,8 @@ static const char *diff_index_args[] = {
3435

3536

3637
struct commit_name {
38+
struct commit_name *next;
39+
unsigned char peeled[20];
3740
struct tag *tag;
3841
unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
3942
unsigned name_checked:1;
@@ -44,6 +47,21 @@ static const char *prio_names[] = {
4447
"head", "lightweight", "annotated",
4548
};
4649

50+
static inline unsigned int hash_sha1(const unsigned char *sha1)
51+
{
52+
unsigned int hash;
53+
memcpy(&hash, sha1, sizeof(hash));
54+
return hash;
55+
}
56+
57+
static inline struct commit_name *find_commit_name(const unsigned char *peeled)
58+
{
59+
struct commit_name *n = lookup_hash(hash_sha1(peeled), &names);
60+
while (n && !!hashcmp(peeled, n->peeled))
61+
n = n->next;
62+
return n;
63+
}
64+
4765
static int replace_name(struct commit_name *e,
4866
int prio,
4967
const unsigned char *sha1,
@@ -82,20 +100,29 @@ static void add_to_known_names(const char *path,
82100
int prio,
83101
const unsigned char *sha1)
84102
{
85-
struct commit_name *e = commit->util;
103+
const unsigned char *peeled = commit->object.sha1;
104+
struct commit_name *e = find_commit_name(peeled);
86105
struct tag *tag = NULL;
87106
if (replace_name(e, prio, sha1, &tag)) {
88107
if (!e) {
108+
void **pos;
89109
e = xmalloc(sizeof(struct commit_name));
90110
commit->util = e;
111+
hashcpy(e->peeled, peeled);
112+
pos = insert_hash(hash_sha1(peeled), e, &names);
113+
if (pos) {
114+
e->next = *pos;
115+
*pos = e;
116+
} else {
117+
e->next = NULL;
118+
}
91119
}
92120
e->tag = tag;
93121
e->prio = prio;
94122
e->name_checked = 0;
95123
hashcpy(e->sha1, sha1);
96124
e->path = path;
97125
}
98-
found_names = 1;
99126
}
100127

101128
static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
@@ -240,7 +267,7 @@ static void describe(const char *arg, int last_one)
240267
if (!cmit)
241268
die("%s is not a valid '%s' object", arg, commit_type);
242269

243-
n = cmit->util;
270+
n = find_commit_name(cmit->object.sha1);
244271
if (n && (tags || all || n->prio == 2)) {
245272
/*
246273
* Exact match to an existing ref.
@@ -418,8 +445,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
418445
return cmd_name_rev(i + argc, args, prefix);
419446
}
420447

448+
init_hash(&names);
421449
for_each_rawref(get_name, NULL);
422-
if (!found_names && !always)
450+
if (!names.nr && !always)
423451
die("No names found, cannot describe anything.");
424452

425453
if (argc == 0) {

0 commit comments

Comments
 (0)