Skip to content

Commit c3e3cd4

Browse files
spearceJunio C Hamano
authored andcommitted
Hash tags by commit SHA1 in git-describe.
If a project has a very large number of tags then git-describe will spend a good part of its time looping over the tags testing them one at a time to determine if it matches a given commit. For 10 tags this is not a big deal, but for hundreds of tags the time could become considerable if we don't find an exact match for the input commit and we need to walk back along the history chain. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dccd0c2 commit c3e3cd4

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

builtin-describe.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ static const char describe_usage[] =
1212

1313
static int all; /* Default to annotated tags only */
1414
static int tags; /* But allow any tags if --tags is specified */
15-
1615
static int abbrev = DEFAULT_ABBREV;
1716

18-
static int names, allocs;
17+
static unsigned int names[256], allocs[256];
1918
static struct commit_name {
2019
struct commit *commit;
2120
int prio; /* annotated tag = 2, tag = 1, head = 0 */
2221
char path[FLEX_ARRAY]; /* more */
23-
} **name_array = NULL;
22+
} **name_array[256];
2423

2524
static struct commit_name *match(struct commit *cmit)
2625
{
27-
int i = names;
28-
struct commit_name **p = name_array;
26+
unsigned char m = cmit->object.sha1[0];
27+
unsigned int i = names[m];
28+
struct commit_name **p = name_array[m];
2929

3030
while (i-- > 0) {
3131
struct commit_name *n = *p++;
@@ -42,17 +42,19 @@ static void add_to_known_names(const char *path,
4242
int idx;
4343
int len = strlen(path)+1;
4444
struct commit_name *name = xmalloc(sizeof(struct commit_name) + len);
45+
unsigned char m = commit->object.sha1[0];
4546

4647
name->commit = commit;
4748
name->prio = prio;
4849
memcpy(name->path, path, len);
49-
idx = names;
50-
if (idx >= allocs) {
51-
allocs = (idx + 50) * 3 / 2;
52-
name_array = xrealloc(name_array, allocs*sizeof(*name_array));
50+
idx = names[m];
51+
if (idx >= allocs[m]) {
52+
allocs[m] = (idx + 50) * 3 / 2;
53+
name_array[m] = xrealloc(name_array[m],
54+
allocs[m] * sizeof(*name_array));
5355
}
54-
name_array[idx] = name;
55-
names = ++idx;
56+
name_array[m][idx] = name;
57+
names[m] = ++idx;
5658
}
5759

5860
static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
@@ -121,9 +123,12 @@ static void describe(const char *arg, int last_one)
121123
die("%s is not a valid '%s' object", arg, commit_type);
122124

123125
if (!initialized) {
126+
unsigned int m;
124127
initialized = 1;
125128
for_each_ref(get_name, NULL);
126-
qsort(name_array, names, sizeof(*name_array), compare_names);
129+
for (m = 0; m < ARRAY_SIZE(name_array); m++)
130+
qsort(name_array[m], names[m],
131+
sizeof(*name_array[m]), compare_names);
127132
}
128133

129134
n = match(cmit);

0 commit comments

Comments
 (0)