@matheusbsbs / matheus-bagliano / commits / 1c4fea3a40
Fork 0

git-rev-list --bisect: optimization

This improves the performance of revision bisection. The idea is to avoid rather expensive count_distance() function, which counts the number of commits that are reachable from any given commit (including itself) in the set. When a commit has only one relevant parent commit, the number of commits the commit can reach is exactly the number of commits that the parent can reach plus one; instead of running count_distance() on commits that are on straight single strand of pearls, we can just add one to the parents' count. On the other hand, for a merge commit, because the commits reachable from one parent can be reachable from another parent, you cannot just add the parents' counts up plus one for the commit itself; that would overcount ancestors that are reachable from more than one parents. The algorithm used in the patch runs count_distance() on merge commits, and uses the util field of commit objects to remember them. After that, the number of commits reachable from each of the remaining commits is counted by finding a commit whose count is not yet known but the count for its (sole) parent is known, and adding one to the parent's count, until we assign numbers to everybody. Another small optimization is whenever we find a half-way commit (that is, a commit that can reach exactly half of the commits), we stop giving counts to remaining commits, as we will not find any better commit than we just found. The performance to bisect between v1.0.0 and v1.5.0 in git.git repository was improved by saying good and bad in turns from 3.68 seconds down to 1.26 seconds. Bisecting the kernel between v2.6.18 and v2.6.20 was sped up from 21.84 seconds down to 4.22 seconds. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 21, 2007 at 22:16 UTC 1c4fea3a40e836dcee2f16091bf7bfba96c924d0
1 file changed +160 -2
builtin-rev-list.c
+160 -2
@@ -207,6 +207,160 @@ static struct commit_list *find_bisection(struct commit_list *list,
207 return best;
208 }
209
210 +static inline int commit_interesting(struct commit_list *elem)
211 +{
212 + unsigned flags = elem->item->object.flags;
213 + if (flags & UNINTERESTING)
214 + return 0;
215 + return (!revs.prune_fn || (flags & TREECHANGE));
216 +}
217 +
218 +static inline int weight(struct commit_list *elem)
219 +{
220 + return *((int*)(elem->item->util));
221 +}
222 +
223 +static inline void weight_set(struct commit_list *elem, int weight)
224 +{
225 + *((int*)(elem->item->util)) = weight;
226 +}
227 +
228 +static int count_interesting_parents(struct commit_list *elem)
229 +{
230 + int cnt = 0;
231 + if (!elem->item->parents)
232 + return cnt;
233 + for (elem = elem->item->parents; elem; elem = elem->next) {
234 + if (commit_interesting(elem))
235 + cnt++;
236 + }
237 + return cnt;
238 +}
239 +
240 +static struct commit_list *find_bisection_2(struct commit_list *list,
241 + int *reaches, int *all)
242 +{
243 + int n, nr, counted, distance;
244 + struct commit_list *p, *best;
245 + int *weights;
246 +
247 + for (nr = 0, p = list; p; p = p->next) {
248 + if (commit_interesting(p))
249 + nr++;
250 + }
251 + *all = nr;
252 + weights = xcalloc(nr, sizeof(int*));
253 + counted = 0;
254 +
255 + for (n = 0, p = list; p; p = p->next) {
256 + if (!commit_interesting(p))
257 + continue;
258 + if (commit_interesting(p)) {
259 + /*
260 + * positive weight is the number of interesting
261 + * commits it can reach, including itself.
262 + * weight = 0 means it has one parent and
263 + * its distance is unknown.
264 + * weight < 0 means it has more than one
265 + * parent and its distance is unknown.
266 + */
267 + p->item->util = &weights[n++];
268 + switch (count_interesting_parents(p)) {
269 + case 0:
270 + weight_set(p, 1);
271 + counted++;
272 + break;
273 + case 1:
274 + weight_set(p, 0);
275 + break;
276 + default:
277 + weight_set(p, -1);
278 + break;
279 + }
280 + }
281 + }
282 +
283 + /*
284 + * If you have only one parent in the resulting set
285 + * then you can reach one commit more than that parent
286 + * can reach. So we do not have to run the expensive
287 + * count_distance() for single strand of pearls.
288 + *
289 + * However, if you have more than one parents, you cannot
290 + * just add their distance and one for yourself, since
291 + * they usually reach the same ancestor and you would
292 + * end up counting them twice that way.
293 + *
294 + * So we will first count distance of merges the usual
295 + * way, and then fill the blanks using cheaper algorithm.
296 + */
297 + for (p = list; p; p = p->next) {
298 + if (!commit_interesting(p))
299 + continue;
300 + n = weight(p);
301 + if (0 <= n)
302 + continue;
303 + distance = count_distance(p);
304 + clear_distance(p);
305 + weight_set(p, distance);
306 +
307 + /* Does it happen to be at exactly half-way? */
308 + distance *= 2;
309 + if (nr == distance || (nr+1) == distance) {
310 + p->next = NULL;
311 + *reaches = weight(p);
312 + free(weights);
313 + return p;
314 + }
315 + counted++;
316 + }
317 +
318 + while (counted < nr) {
319 + for (p = list; p; p = p->next) {
320 + struct commit_list *q;
321 +
322 + if (!commit_interesting(p) || 0 < weight(p))
323 + continue;
324 + for (q = p->item->parents; q; q = q->next)
325 + if (commit_interesting(q) && 0 < weight(q))
326 + break;
327 + if (!q)
328 + continue;
329 + weight_set(p, weight(q)+1);
330 + counted++;
331 +
332 + /* Does it happen to be at exactly half-way? */
333 + distance = weight(p) * 2;
334 + if (nr == distance || (nr+1) == distance) {
335 + p->next = NULL;
336 + *reaches = weight(p);
337 + free(weights);
338 + return p;
339 + }
340 + }
341 + }
342 +
343 + /* Then find the best one */
344 + counted = 0;
345 + best = list;
346 + for (p = list; p; p = p->next) {
347 + if (!commit_interesting(p))
348 + continue;
349 + distance = weight(p);
350 + if (nr - distance < distance)
351 + distance = nr - distance;
352 + if (distance > counted) {
353 + best = p;
354 + counted = distance;
355 + *reaches = weight(p);
356 + }
357 + }
358 + if (best)
359 + best->next = NULL;
360 + free(weights);
361 + return best;
362 +}
363 +
364 static void read_revisions_from_stdin(struct rev_info *revs)
365 {
366 char line[1000];
@@ -298,8 +452,12 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
452 if (bisect_list) {
453 int reaches = reaches, all = all;
454
301 - revs.commits = find_bisection(revs.commits,
302 - &reaches, &all);
455 + if (!revs.prune_fn)
456 + revs.commits = find_bisection_2(revs.commits,
457 + &reaches, &all);
458 + else
459 + revs.commits = find_bisection(revs.commits,
460 + &reaches, &all);
461 if (bisect_show_vars) {
462 int cnt;
463 if (!revs.commits)