@matheusbsbs / matheus-bagliano / commits / e7eb50347b
Fork 0

Remove hash in git-describe in favor of util slot.

Currently we don't use the util field of struct commit but we want fast access to the highest priority name that references any given commit object during our matching loop. A really simple approach is to just store the name directly in the util field. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Jan 14, 2007 at 22:16 UTC e7eb50347bba54ab52f9740a3bbb6e833bdadf6e
1 file changed +12 -64
builtin-describe.c
+12 -64
@@ -16,60 +16,27 @@ static int tags; /* But allow any tags if --tags is specified */
16 static int abbrev = DEFAULT_ABBREV;
17 static int max_candidates = 10;
18
19 -static unsigned int names[256], allocs[256];
20 -static struct commit_name {
21 - struct commit *commit;
19 +struct commit_name {
20 int prio; /* annotated tag = 2, tag = 1, head = 0 */
21 char path[FLEX_ARRAY]; /* more */
24 -} **name_array[256];
22 +};
23 static const char *prio_names[] = {
24 "head", "lightweight", "annotated",
25 };
26
29 -static struct commit_name *match(struct commit *cmit)
30 -{
31 - unsigned char level0 = cmit->object.sha1[0];
32 - struct commit_name **p = name_array[level0];
33 - unsigned int hi = names[level0];
34 - unsigned int lo = 0;
35 -
36 - while (lo < hi) {
37 - unsigned int mi = (lo + hi) / 2;
38 - int cmp = hashcmp(p[mi]->commit->object.sha1,
39 - cmit->object.sha1);
40 - if (!cmp) {
41 - while (mi && p[mi - 1]->commit == cmit)
42 - mi--;
43 - return p[mi];
44 - }
45 - if (cmp > 0)
46 - hi = mi;
47 - else
48 - lo = mi+1;
49 - }
50 - return NULL;
51 -}
52 -
27 static void add_to_known_names(const char *path,
28 struct commit *commit,
29 int prio)
30 {
57 - int idx;
58 - int len = strlen(path)+1;
59 - struct commit_name *name = xmalloc(sizeof(struct commit_name) + len);
60 - unsigned char m = commit->object.sha1[0];
61 -
62 - name->commit = commit;
63 - name->prio = prio;
64 - memcpy(name->path, path, len);
65 - idx = names[m];
66 - if (idx >= allocs[m]) {
67 - allocs[m] = (idx + 50) * 3 / 2;
68 - name_array[m] = xrealloc(name_array[m],
69 - allocs[m] * sizeof(*name_array));
31 + struct commit_name *e = commit->util;
32 + if (!e || e->prio < prio) {
33 + size_t len = strlen(path)+1;
34 + free(e);
35 + e = xmalloc(sizeof(struct commit_name) + len);
36 + e->prio = prio;
37 + memcpy(e->path, path, len);
38 + commit->util = e;
39 }
71 - name_array[m][idx] = name;
72 - names[m] = ++idx;
40 }
41
42 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
@@ -104,21 +71,6 @@ static int get_name(const char *path, const unsigned char *sha1, int flag, void
71 return 0;
72 }
73
107 -static int compare_names(const void *_a, const void *_b)
108 -{
109 - struct commit_name *a = *(struct commit_name **)_a;
110 - struct commit_name *b = *(struct commit_name **)_b;
111 - unsigned long a_date = a->commit->date;
112 - unsigned long b_date = b->commit->date;
113 - int cmp = hashcmp(a->commit->object.sha1, b->commit->object.sha1);
114 -
115 - if (cmp)
116 - return cmp;
117 - if (a->prio != b->prio)
118 - return b->prio - a->prio;
119 - return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
120 -}
121 -
74 struct possible_tag {
75 struct commit_name *name;
76 int depth;
@@ -158,15 +110,11 @@ static void describe(const char *arg, int last_one)
110 die("%s is not a valid '%s' object", arg, commit_type);
111
112 if (!initialized) {
161 - unsigned int m;
113 initialized = 1;
114 for_each_ref(get_name, NULL);
164 - for (m = 0; m < ARRAY_SIZE(name_array); m++)
165 - qsort(name_array[m], names[m],
166 - sizeof(*name_array[m]), compare_names);
115 }
116
169 - n = match(cmit);
117 + n = cmit->util;
118 if (n) {
119 printf("%s\n", n->path);
120 return;
@@ -182,7 +130,7 @@ static void describe(const char *arg, int last_one)
130 struct commit *c = pop_commit(&list);
131 struct commit_list *parents = c->parents;
132 seen_commits++;
185 - n = match(c);
133 + n = c->util;
134 if (n) {
135 if (match_cnt < max_candidates) {
136 struct possible_tag *t = &all_matches[match_cnt++];