Skip to content

Commit 910c0d7

Browse files
spearceJunio C Hamano
authored andcommitted
Use binary searching on large buckets in git-describe.
If a project has a really huge number of tags (such as several thousand tags) then we are likely to have nearly a hundred tags in some buckets. Scanning those buckets as linked lists could take a large amount of time if done repeatedly during history traversal. Since we are searching for a unique commit SHA1 we can sort all tags by commit SHA1 and perform a binary search within the bucket. Once we identify a particular tag as matching this commit we walk backwards within the bucket matches to make sure we pick up the highest priority tag for that commit, as the binary search may have landed us in the middle of a set of tags which point at the same commit. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3e3cd4 commit 910c0d7

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

builtin-describe.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,24 @@ static struct commit_name {
2323

2424
static struct commit_name *match(struct commit *cmit)
2525
{
26-
unsigned char m = cmit->object.sha1[0];
27-
unsigned int i = names[m];
28-
struct commit_name **p = name_array[m];
29-
30-
while (i-- > 0) {
31-
struct commit_name *n = *p++;
32-
if (n->commit == cmit)
33-
return n;
26+
unsigned char level0 = cmit->object.sha1[0];
27+
struct commit_name **p = name_array[level0];
28+
unsigned int hi = names[level0];
29+
unsigned int lo = 0;
30+
31+
while (lo < hi) {
32+
unsigned int mi = (lo + hi) / 2;
33+
int cmp = hashcmp(p[mi]->commit->object.sha1,
34+
cmit->object.sha1);
35+
if (!cmp) {
36+
while (mi && p[mi - 1]->commit == cmit)
37+
mi--;
38+
return p[mi];
39+
}
40+
if (cmp > 0)
41+
hi = mi;
42+
else
43+
lo = mi+1;
3444
}
3545
return NULL;
3646
}
@@ -95,7 +105,10 @@ static int compare_names(const void *_a, const void *_b)
95105
struct commit_name *b = *(struct commit_name **)_b;
96106
unsigned long a_date = a->commit->date;
97107
unsigned long b_date = b->commit->date;
108+
int cmp = hashcmp(a->commit->object.sha1, b->commit->object.sha1);
98109

110+
if (cmp)
111+
return cmp;
99112
if (a->prio != b->prio)
100113
return b->prio - a->prio;
101114
return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;

0 commit comments

Comments
 (0)