Skip to content

Commit 2557c11

Browse files
committed
Merge branch 'sg/bisect-approximately-halfway'
"git bisect start/next" in a large span of history spends a lot of time trying to come up with exactly the half-way point; this can be optimized by stopping when we see a commit that is close enough to the half-way point. * sg/bisect-approximately-halfway: bisect: loosen halfway() check for a large number of commits
2 parents fd6445a + 0afcea7 commit 2557c11

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

bisect.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ static int count_interesting_parents(struct commit *commit, unsigned bisect_flag
103103
return count;
104104
}
105105

106-
static inline int halfway(struct commit_list *p, int nr)
106+
static inline int approx_halfway(struct commit_list *p, int nr)
107107
{
108+
int diff;
109+
108110
/*
109111
* Don't short-cut something we are not going to return!
110112
*/
@@ -113,13 +115,22 @@ static inline int halfway(struct commit_list *p, int nr)
113115
if (DEBUG_BISECT)
114116
return 0;
115117
/*
116-
* 2 and 3 are halfway of 5.
118+
* For small number of commits 2 and 3 are halfway of 5, and
117119
* 3 is halfway of 6 but 2 and 4 are not.
118120
*/
119-
switch (2 * weight(p) - nr) {
121+
diff = 2 * weight(p) - nr;
122+
switch (diff) {
120123
case -1: case 0: case 1:
121124
return 1;
122125
default:
126+
/*
127+
* For large number of commits we are not so strict, it's
128+
* good enough if it's within ~0.1% of the halfway point,
129+
* e.g. 5000 is exactly halfway of 10000, but we consider
130+
* the values [4996, 5004] as halfway as well.
131+
*/
132+
if (abs(diff) < nr / 1024)
133+
return 1;
123134
return 0;
124135
}
125136
}
@@ -321,8 +332,9 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
321332
weight_set(p, count_distance(p));
322333
clear_distance(list);
323334

324-
/* Does it happen to be at exactly half-way? */
325-
if (!(bisect_flags & FIND_BISECTION_ALL) && halfway(p, nr))
335+
/* Does it happen to be at half-way? */
336+
if (!(bisect_flags & FIND_BISECTION_ALL) &&
337+
approx_halfway(p, nr))
326338
return p;
327339
counted++;
328340
}
@@ -362,8 +374,9 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
362374
else
363375
weight_set(p, weight(q));
364376

365-
/* Does it happen to be at exactly half-way? */
366-
if (!(bisect_flags & FIND_BISECTION_ALL) && halfway(p, nr))
377+
/* Does it happen to be at half-way? */
378+
if (!(bisect_flags & FIND_BISECTION_ALL) &&
379+
approx_halfway(p, nr))
367380
return p;
368381
}
369382
}

0 commit comments

Comments
 (0)