make the previous optimization work also on path-limited rev-list --bisect
The trick is to give a child commit that is not tree-changing the same depth as its parent, so that the depth is propagated properly along strand of pearls. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Mar 23, 2007 at 17:54 UTC
1daa09d9a833d3969b64e763865a58df56bc3871
1 file changed
+151
-98
builtin-rev-list.c
+151
-98
@@ -169,51 +169,7 @@ static void clear_distance(struct commit_list *list)
169
}
170
}
171
172
-static struct commit_list *find_bisection(struct commit_list *list,
173
- int *reaches, int *all)
174
-{
175
- int nr, closest;
176
- struct commit_list *p, *best;
177
-
178
- nr = 0;
179
- p = list;
180
- while (p) {
181
- if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
182
- nr++;
183
- p = p->next;
184
- }
185
- closest = -1;
186
- best = list;
187
- *all = nr;
188
-
189
- for (p = list; p; p = p->next) {
190
- int distance, reach;
191
-
192
- if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
193
- continue;
194
-
195
- distance = reach = count_distance(p);
196
- clear_distance(list);
197
- if (nr - distance < distance)
198
- distance = nr - distance;
199
- if (distance > closest) {
200
- best = p;
201
- *reaches = reach;
202
- closest = distance;
203
- }
204
- }
205
- if (best)
206
- best->next = NULL;
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
-}
172
+#define DEBUG_BISECT 0
173
174
static inline int weight(struct commit_list *elem)
175
{
@@ -225,16 +181,17 @@ static inline void weight_set(struct commit_list *elem, int weight)
181
*((int*)(elem->item->util)) = weight;
182
}
183
228
-static int count_interesting_parents(struct commit_list *elem)
184
+static int count_interesting_parents(struct commit *commit)
185
{
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++;
186
+ struct commit_list *p;
187
+ int count;
188
+
189
+ for (count = 0, p = commit->parents; p; p = p->next) {
190
+ if (p->item->object.flags & UNINTERESTING)
191
+ continue;
192
+ count++;
193
}
237
- return cnt;
194
+ return count;
195
}
196
197
static inline int halfway(struct commit_list *p, int distance, int nr)
@@ -244,7 +201,8 @@ static inline int halfway(struct commit_list *p, int distance, int nr)
201
*/
202
if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
203
return 0;
247
-
204
+ if (DEBUG_BISECT)
205
+ return 0;
206
/*
207
* 2 and 3 are halfway of 5.
208
* 3 is halfway of 6 but 2 and 4 are not.
@@ -258,49 +216,127 @@ static inline int halfway(struct commit_list *p, int distance, int nr)
216
}
217
}
218
261
-static struct commit_list *find_bisection_2(struct commit_list *list,
262
- int *reaches, int *all)
219
+#if !DEBUG_BISECT
220
+#define show_list(a,b,c,d) do { ; } while (0)
221
+#else
222
+static void show_list(const char *debug, int counted, int nr,
223
+ struct commit_list *list)
224
{
264
- int n, nr, counted, distance;
265
- struct commit_list *p, *best;
225
+ struct commit_list *p;
226
+
227
+ fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
228
+
229
+ for (p = list; p; p = p->next) {
230
+ struct commit_list *pp;
231
+ struct commit *commit = p->item;
232
+ unsigned flags = commit->object.flags;
233
+ enum object_type type;
234
+ unsigned long size;
235
+ char *buf = read_sha1_file(commit->object.sha1, &type, &size);
236
+ char *ep, *sp;
237
+
238
+ fprintf(stderr, "%c%c%c ",
239
+ (flags & TREECHANGE) ? 'T' : ' ',
240
+ (flags & UNINTERESTING) ? 'U' : ' ',
241
+ (flags & COUNTED) ? 'C' : ' ');
242
+ if (commit->util)
243
+ fprintf(stderr, "%3d", weight(p));
244
+ else
245
+ fprintf(stderr, "---");
246
+ fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
247
+ for (pp = commit->parents; pp; pp = pp->next)
248
+ fprintf(stderr, " %.*s", 8,
249
+ sha1_to_hex(pp->item->object.sha1));
250
+
251
+ sp = strstr(buf, "\n\n");
252
+ if (sp) {
253
+ sp += 2;
254
+ for (ep = sp; *ep && *ep != '\n'; ep++)
255
+ ;
256
+ fprintf(stderr, " %.*s", (int)(ep - sp), sp);
257
+ }
258
+ fprintf(stderr, "\n");
259
+ }
260
+}
261
+#endif /* DEBUG_BISECT */
262
+
263
+/*
264
+ * zero or positive weight is the number of interesting commits it can
265
+ * reach, including itself. Especially, weight = 0 means it does not
266
+ * reach any tree-changing commits (e.g. just above uninteresting one
267
+ * but traversal is with pathspec).
268
+ *
269
+ * weight = -1 means it has one parent and its distance is yet to
270
+ * be computed.
271
+ *
272
+ * weight = -2 means it has more than one parent and its distance is
273
+ * unknown. After running count_distance() first, they will get zero
274
+ * or positive distance.
275
+ */
276
+
277
+static struct commit_list *find_bisection(struct commit_list *list,
278
+ int *reaches, int *all)
279
+{
280
+ int n, nr, on_list, counted, distance;
281
+ struct commit_list *p, *best, *next, *last;
282
int *weights;
283
268
- for (nr = 0, p = list; p; p = p->next) {
269
- if (commit_interesting(p))
284
+ show_list("bisection 2 entry", 0, 0, list);
285
+
286
+ /*
287
+ * Count the number of total and tree-changing items on the
288
+ * list, while reversing the list.
289
+ */
290
+ for (nr = on_list = 0, last = NULL, p = list;
291
+ p;
292
+ p = next) {
293
+ unsigned flags = p->item->object.flags;
294
+
295
+ next = p->next;
296
+ if (flags & UNINTERESTING)
297
+ continue;
298
+ p->next = last;
299
+ last = p;
300
+ if (!revs.prune_fn || (flags & TREECHANGE))
301
nr++;
302
+ on_list++;
303
}
304
+ list = last;
305
+ show_list("bisection 2 sorted", 0, nr, list);
306
+
307
*all = nr;
273
- weights = xcalloc(nr, sizeof(int*));
308
+ weights = xcalloc(on_list, sizeof(int*));
309
counted = 0;
310
311
for (n = 0, p = list; p; p = p->next) {
277
- if (!commit_interesting(p))
278
- continue;
279
- if (commit_interesting(p)) {
280
- /*
281
- * positive weight is the number of interesting
282
- * commits it can reach, including itself.
283
- * weight = 0 means it has one parent and
284
- * its distance is unknown.
285
- * weight < 0 means it has more than one
286
- * parent and its distance is unknown.
287
- */
288
- p->item->util = &weights[n++];
289
- switch (count_interesting_parents(p)) {
290
- case 0:
312
+ struct commit *commit = p->item;
313
+ unsigned flags = commit->object.flags;
314
+
315
+ p->item->util = &weights[n++];
316
+ switch (count_interesting_parents(commit)) {
317
+ case 0:
318
+ if (!revs.prune_fn || (flags & TREECHANGE)) {
319
weight_set(p, 1);
320
counted++;
293
- break;
294
- case 1:
295
- weight_set(p, 0);
296
- break;
297
- default:
298
- weight_set(p, -1);
299
- break;
321
+ show_list("bisection 2 count one",
322
+ counted, nr, list);
323
}
324
+ /*
325
+ * otherwise, it is known not to reach any
326
+ * tree-changing commit and gets weight 0.
327
+ */
328
+ break;
329
+ case 1:
330
+ weight_set(p, -1);
331
+ break;
332
+ default:
333
+ weight_set(p, -2);
334
+ break;
335
}
336
}
337
338
+ show_list("bisection 2 initialize", counted, nr, list);
339
+
340
/*
341
* If you have only one parent in the resulting set
342
* then you can reach one commit more than that parent
@@ -316,13 +352,13 @@ static struct commit_list *find_bisection_2(struct commit_list *list,
352
* way, and then fill the blanks using cheaper algorithm.
353
*/
354
for (p = list; p; p = p->next) {
319
- if (!commit_interesting(p))
355
+ if (p->item->object.flags & UNINTERESTING)
356
continue;
357
n = weight(p);
322
- if (0 <= n)
358
+ if (n != -2)
359
continue;
360
distance = count_distance(p);
325
- clear_distance(p);
361
+ clear_distance(list);
362
weight_set(p, distance);
363
364
/* Does it happen to be at exactly half-way? */
@@ -335,19 +371,37 @@ static struct commit_list *find_bisection_2(struct commit_list *list,
371
counted++;
372
}
373
374
+ show_list("bisection 2 count_distance", counted, nr, list);
375
+
376
while (counted < nr) {
377
for (p = list; p; p = p->next) {
378
struct commit_list *q;
379
+ unsigned flags = p->item->object.flags;
380
342
- if (!commit_interesting(p) || 0 < weight(p))
381
+ if (0 <= weight(p))
382
continue;
344
- for (q = p->item->parents; q; q = q->next)
345
- if (commit_interesting(q) && 0 < weight(q))
383
+ for (q = p->item->parents; q; q = q->next) {
384
+ if (q->item->object.flags & UNINTERESTING)
385
+ continue;
386
+ if (0 <= weight(q))
387
break;
388
+ }
389
if (!q)
390
continue;
349
- weight_set(p, weight(q)+1);
350
- counted++;
391
+
392
+ /*
393
+ * weight for p is unknown but q is known.
394
+ * add one for p itself if p is to be counted,
395
+ * otherwise inherit it from q directly.
396
+ */
397
+ if (!revs.prune_fn || (flags & TREECHANGE)) {
398
+ weight_set(p, weight(q)+1);
399
+ counted++;
400
+ show_list("bisection 2 count one",
401
+ counted, nr, list);
402
+ }
403
+ else
404
+ weight_set(p, weight(q));
405
406
/* Does it happen to be at exactly half-way? */
407
distance = weight(p);
@@ -360,11 +414,15 @@ static struct commit_list *find_bisection_2(struct commit_list *list,
414
}
415
}
416
417
+ show_list("bisection 2 counted all", counted, nr, list);
418
+
419
/* Then find the best one */
364
- counted = 0;
420
+ counted = -1;
421
best = list;
422
for (p = list; p; p = p->next) {
367
- if (!commit_interesting(p))
423
+ unsigned flags = p->item->object.flags;
424
+
425
+ if (revs.prune_fn && !(flags & TREECHANGE))
426
continue;
427
distance = weight(p);
428
if (nr - distance < distance)
@@ -472,12 +530,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
530
if (bisect_list) {
531
int reaches = reaches, all = all;
532
475
- if (!revs.prune_fn)
476
- revs.commits = find_bisection_2(revs.commits,
477
- &reaches, &all);
478
- else
479
- revs.commits = find_bisection(revs.commits,
480
- &reaches, &all);
533
+ revs.commits = find_bisection(revs.commits, &reaches, &all);
534
if (bisect_show_vars) {
535
int cnt;
536
if (!revs.commits)