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

refs.c: add a function to sort a ref list, rather then sorting on add

Rather than sorting the refs list while building it, sort in one go after it is built using a merge sort. This has a large performance boost with large numbers of refs. It shouldn't happen that we read duplicate entries into the same list, but just in case sort_ref_list drops them if the SHA1s are the same, or dies, as we have no way of knowing which one is the correct one. Signed-off-by: Julian Phillips <julian@quantumfyre.co.uk> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Julian Phillips committed Apr 17, 2007 at 02:42 UTC c774aab98ce6c5ef7aaacbef38da0a501eb671d4
1 file changed +89 -21
refs.c
+89 -21
@@ -47,22 +47,7 @@ static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
47 struct ref_list **new_entry)
48 {
49 int len;
50 - struct ref_list **p = &list, *entry;
51 -
52 - /* Find the place to insert the ref into.. */
53 - while ((entry = *p) != NULL) {
54 - int cmp = strcmp(entry->name, name);
55 - if (cmp > 0)
56 - break;
57 -
58 - /* Same as existing entry? */
59 - if (!cmp) {
60 - if (new_entry)
61 - *new_entry = entry;
62 - return list;
63 - }
64 - p = &entry->next;
65 - }
50 + struct ref_list *entry;
51
52 /* Allocate it and add it in.. */
53 len = strlen(name) + 1;
@@ -71,11 +56,94 @@ static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
56 hashclr(entry->peeled);
57 memcpy(entry->name, name, len);
58 entry->flag = flag;
74 - entry->next = *p;
75 - *p = entry;
59 + entry->next = list;
60 if (new_entry)
61 *new_entry = entry;
78 - return list;
62 + return entry;
63 +}
64 +
65 +/* merge sort the ref list */
66 +static struct ref_list *sort_ref_list(struct ref_list *list)
67 +{
68 + int psize, qsize, last_merge_count, cmp;
69 + struct ref_list *p, *q, *l, *e;
70 + struct ref_list *new_list = list;
71 + int k = 1;
72 + int merge_count = 0;
73 +
74 + if (!list)
75 + return list;
76 +
77 + do {
78 + last_merge_count = merge_count;
79 + merge_count = 0;
80 +
81 + psize = 0;
82 +
83 + p = new_list;
84 + q = new_list;
85 + new_list = NULL;
86 + l = NULL;
87 +
88 + while (p) {
89 + merge_count++;
90 +
91 + while (psize < k && q->next) {
92 + q = q->next;
93 + psize++;
94 + }
95 + qsize = k;
96 +
97 + while ((psize > 0) || (qsize > 0 && q)) {
98 + if (qsize == 0 || !q) {
99 + e = p;
100 + p = p->next;
101 + psize--;
102 + } else if (psize == 0) {
103 + e = q;
104 + q = q->next;
105 + qsize--;
106 + } else {
107 + cmp = strcmp(q->name, p->name);
108 + if (cmp < 0) {
109 + e = q;
110 + q = q->next;
111 + qsize--;
112 + } else if (cmp > 0) {
113 + e = p;
114 + p = p->next;
115 + psize--;
116 + } else {
117 + if (hashcmp(q->sha1, p->sha1))
118 + die("Duplicated ref, and SHA1s don't match: %s",
119 + q->name);
120 + warning("Duplicated ref: %s", q->name);
121 + e = q;
122 + q = q->next;
123 + qsize--;
124 + free(e);
125 + e = p;
126 + p = p->next;
127 + psize--;
128 + }
129 + }
130 +
131 + e->next = NULL;
132 +
133 + if (l)
134 + l->next = e;
135 + if (!new_list)
136 + new_list = e;
137 + l = e;
138 + }
139 +
140 + p = q;
141 + };
142 +
143 + k = k * 2;
144 + } while ((last_merge_count != merge_count) || (last_merge_count != 1));
145 +
146 + return new_list;
147 }
148
149 /*
@@ -142,7 +210,7 @@ static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
210 !get_sha1_hex(refline + 1, sha1))
211 hashcpy(last->peeled, sha1);
212 }
145 - cached_refs->packed = list;
213 + cached_refs->packed = sort_ref_list(list);
214 }
215
216 static struct ref_list *get_packed_refs(void)
@@ -201,7 +269,7 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
269 free(ref);
270 closedir(dir);
271 }
204 - return list;
272 + return sort_ref_list(list);
273 }
274
275 static struct ref_list *get_loose_refs(void)