describe: Delay looking up commits until searching for an inexact match
Now that struct commit.util is not used until after we've checked that the argument doesn't exactly match a tag, we can wait until then to look up the commits for each tag. This avoids a lot of I/O on --exact-match queries in repositories with many tags. For example, 'git describe --exact-match HEAD' becomes about 12 times faster on a cold cache (3.2s instead of 39s) in a linux-2.6 repository with 2000 packed tags. That is a huge win for the interactivity of the __git_ps1 shell prompt helper when on a detached HEAD. Signed-off-by: Anders Kaseorg <andersk@ksplice.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Anders Kaseorg committed
Dec 9, 2010 at 01:47 UTC
d1645d02defe43fa5b43140050ba265fcc49fa70
1 file changed
+22
-15
builtin/describe.c
+22
-15
@@ -24,6 +24,7 @@ static int longformat;
24
static int abbrev = DEFAULT_ABBREV;
25
static int max_candidates = 10;
26
static struct hash_table names;
27
+static int have_util;
28
static const char *pattern;
29
static int always;
30
static const char *dirty;
@@ -62,6 +63,17 @@ static inline struct commit_name *find_commit_name(const unsigned char *peeled)
63
return n;
64
}
65
66
+static int set_util(void *chain)
67
+{
68
+ struct commit_name *n;
69
+ for (n = chain; n; n = n->next) {
70
+ struct commit *c = lookup_commit_reference_gently(n->peeled, 1);
71
+ if (c)
72
+ c->util = n;
73
+ }
74
+ return 0;
75
+}
76
+
77
static int replace_name(struct commit_name *e,
78
int prio,
79
const unsigned char *sha1,
@@ -96,18 +108,16 @@ static int replace_name(struct commit_name *e,
108
}
109
110
static void add_to_known_names(const char *path,
99
- struct commit *commit,
111
+ const unsigned char *peeled,
112
int prio,
113
const unsigned char *sha1)
114
{
103
- const unsigned char *peeled = commit->object.sha1;
115
struct commit_name *e = find_commit_name(peeled);
116
struct tag *tag = NULL;
117
if (replace_name(e, prio, sha1, &tag)) {
118
if (!e) {
119
void **pos;
120
e = xmalloc(sizeof(struct commit_name));
110
- commit->util = e;
121
hashcpy(e->peeled, peeled);
122
pos = insert_hash(hash_sha1(peeled), e, &names);
123
if (pos) {
@@ -128,8 +138,6 @@ static void add_to_known_names(const char *path,
138
static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
139
{
140
int might_be_tag = !prefixcmp(path, "refs/tags/");
131
- struct commit *commit;
132
- struct object *object;
141
unsigned char peeled[20];
142
int is_tag, prio;
143
@@ -137,16 +145,10 @@ static int get_name(const char *path, const unsigned char *sha1, int flag, void
145
return 0;
146
147
if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
140
- commit = lookup_commit_reference_gently(peeled, 1);
141
- if (!commit)
142
- return 0;
143
- is_tag = !!hashcmp(sha1, commit->object.sha1);
148
+ is_tag = !!hashcmp(sha1, peeled);
149
} else {
145
- commit = lookup_commit_reference_gently(sha1, 1);
146
- object = parse_object(sha1);
147
- if (!commit || !object)
148
- return 0;
149
- is_tag = object->type == OBJ_TAG;
150
+ hashcpy(peeled, sha1);
151
+ is_tag = 0;
152
}
153
154
/* If --all, then any refs are used.
@@ -169,7 +171,7 @@ static int get_name(const char *path, const unsigned char *sha1, int flag, void
171
if (!prio)
172
return 0;
173
}
172
- add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
174
+ add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
175
return 0;
176
}
177
@@ -286,6 +288,11 @@ static void describe(const char *arg, int last_one)
288
if (debug)
289
fprintf(stderr, "searching to describe %s\n", arg);
290
291
+ if (!have_util) {
292
+ for_each_hash(&names, set_util);
293
+ have_util = 1;
294
+ }
295
+
296
list = NULL;
297
cmit->object.flags = SEEN;
298
commit_list_insert(cmit, &list);