@matheusbsbs / matheus-bagliano / commits / 644797119d
Fork 0

copy vs rename detection: avoid unnecessary O(n*m) loops

The core rename detection had some rather stupid code to check if a pathname was used by a later modification or rename, which basically walked the whole pathname space for all renames for each rename, in order to tell whether it was a pure rename (no remaining users) or should be considered a copy (other users of the source file remaining). That's really silly, since we can just keep a count of users around, and replace all those complex and expensive loops with just testing that simple counter (but this all depends on the previous commit that shared the diff_filespec data structure by using a separate reference count). Note that the reference count is not the same as the rename count: they behave otherwise rather similarly, but the reference count is tied to the allocation (and decremented at de-allocation, so that when it turns zero we can get rid of the memory), while the rename count is tied to the renames and is decremented when we find a rename (so that when it turns zero we know that it was a rename, not a copy). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Oct 25, 2007 at 11:20 UTC 644797119d7a3b7a043a51a9cccd8758f8451f91
3 files changed +35 -75
diff.c
+17 -23
@@ -2597,9 +2597,9 @@ void diff_debug_filepair(const struct diff_filepair *p, int i)
2597 {
2598 diff_debug_filespec(p->one, i, "one");
2599 diff_debug_filespec(p->two, i, "two");
2600 - fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2600 + fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
2601 p->score, p->status ? p->status : '?',
2602 - p->source_stays, p->broken_pair);
2602 + p->one->rename_used, p->broken_pair);
2603 }
2604
2605 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
@@ -2617,8 +2617,8 @@ void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2617
2618 static void diff_resolve_rename_copy(void)
2619 {
2620 - int i, j;
2621 - struct diff_filepair *p, *pp;
2620 + int i;
2621 + struct diff_filepair *p;
2622 struct diff_queue_struct *q = &diff_queued_diff;
2623
2624 diff_debug_queue("resolve-rename-copy", q);
@@ -2640,27 +2640,21 @@ static void diff_resolve_rename_copy(void)
2640 * either in-place edit or rename/copy edit.
2641 */
2642 else if (DIFF_PAIR_RENAME(p)) {
2643 - if (p->source_stays) {
2644 - p->status = DIFF_STATUS_COPIED;
2645 - continue;
2646 - }
2647 - /* See if there is some other filepair that
2648 - * copies from the same source as us. If so
2649 - * we are a copy. Otherwise we are either a
2650 - * copy if the path stays, or a rename if it
2651 - * does not, but we already handled "stays" case.
2643 + /*
2644 + * A rename might have re-connected a broken
2645 + * pair up, causing the pathnames to be the
2646 + * same again. If so, that's not a rename at
2647 + * all, just a modification..
2648 + *
2649 + * Otherwise, see if this source was used for
2650 + * multiple renames, in which case we decrement
2651 + * the count, and call it a copy.
2652 */
2653 - for (j = i + 1; j < q->nr; j++) {
2654 - pp = q->queue[j];
2655 - if (strcmp(pp->one->path, p->one->path))
2656 - continue; /* not us */
2657 - if (!DIFF_PAIR_RENAME(pp))
2658 - continue; /* not a rename/copy */
2659 - /* pp is a rename/copy from the same source */
2653 + if (!strcmp(p->one->path, p->two->path))
2654 + p->status = DIFF_STATUS_MODIFIED;
2655 + else if (--p->one->rename_used > 0)
2656 p->status = DIFF_STATUS_COPIED;
2661 - break;
2662 - }
2663 - if (!p->status)
2657 + else
2658 p->status = DIFF_STATUS_RENAMED;
2659 }
2660 else if (hashcmp(p->one->sha1, p->two->sha1) ||
diffcore-rename.c
+17 -51
@@ -55,12 +55,10 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
55 static struct diff_rename_src {
56 struct diff_filespec *one;
57 unsigned short score; /* to remember the break score */
58 - unsigned src_path_left : 1;
58 } *rename_src;
59 static int rename_src_nr, rename_src_alloc;
60
61 static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
63 - int src_path_left,
62 unsigned short score)
63 {
64 int first, last;
@@ -92,7 +90,6 @@ static struct diff_rename_src *register_rename_src(struct diff_filespec *one,
90 (rename_src_nr - first - 1) * sizeof(*rename_src));
91 rename_src[first].one = one;
92 rename_src[first].score = score;
95 - rename_src[first].src_path_left = src_path_left;
93 return &(rename_src[first]);
94 }
95
@@ -216,6 +213,7 @@ static void record_rename_pair(int dst_index, int src_index, int score)
213 die("internal error: dst already matched.");
214
215 src = rename_src[src_index].one;
216 + src->rename_used++;
217 src->count++;
218
219 dst = rename_dst[dst_index].two;
@@ -227,7 +225,6 @@ static void record_rename_pair(int dst_index, int src_index, int score)
225 dp->score = rename_src[src_index].score;
226 else
227 dp->score = score;
230 - dp->source_stays = rename_src[src_index].src_path_left;
228 rename_dst[dst_index].pair = dp;
229 }
230
@@ -245,21 +242,6 @@ static int score_compare(const void *a_, const void *b_)
242 return b->score - a->score;
243 }
244
248 -static int compute_stays(struct diff_queue_struct *q,
249 - struct diff_filespec *one)
250 -{
251 - int i;
252 - for (i = 0; i < q->nr; i++) {
253 - struct diff_filepair *p = q->queue[i];
254 - if (strcmp(one->path, p->two->path))
255 - continue;
256 - if (DIFF_PAIR_RENAME(p)) {
257 - return 0; /* something else is renamed into this */
258 - }
259 - }
260 - return 1;
261 -}
262 -
245 /*
246 * Find exact renames first.
247 *
@@ -338,15 +320,25 @@ void diffcore_rename(struct diff_options *options)
320 locate_rename_dst(p->two, 1);
321 }
322 else if (!DIFF_FILE_VALID(p->two)) {
341 - /* If the source is a broken "delete", and
323 + /*
324 + * If the source is a broken "delete", and
325 * they did not really want to get broken,
326 * that means the source actually stays.
327 + * So we increment the "rename_used" score
328 + * by one, to indicate ourselves as a user
329 + */
330 + if (p->broken_pair && !p->score)
331 + p->one->rename_used++;
332 + register_rename_src(p->one, p->score);
333 + }
334 + else if (detect_rename == DIFF_DETECT_COPY) {
335 + /*
336 + * Increment the "rename_used" score by
337 + * one, to indicate ourselves as a user.
338 */
345 - int stays = (p->broken_pair && !p->score);
346 - register_rename_src(p->one, stays, p->score);
339 + p->one->rename_used++;
340 + register_rename_src(p->one, p->score);
341 }
348 - else if (detect_rename == DIFF_DETECT_COPY)
349 - register_rename_src(p->one, 1, p->score);
342 }
343 if (rename_dst_nr == 0 || rename_src_nr == 0)
344 goto cleanup; /* nothing to do */
@@ -472,16 +464,7 @@ void diffcore_rename(struct diff_options *options)
464 pair_to_free = p;
465 }
466 else {
475 - for (j = 0; j < rename_dst_nr; j++) {
476 - if (!rename_dst[j].pair)
477 - continue;
478 - if (strcmp(rename_dst[j].pair->
479 - one->path,
480 - p->one->path))
481 - continue;
482 - break;
483 - }
484 - if (j < rename_dst_nr)
467 + if (p->one->rename_used)
468 /* this path remains */
469 pair_to_free = p;
470 }
@@ -507,23 +490,6 @@ void diffcore_rename(struct diff_options *options)
490 *q = outq;
491 diff_debug_queue("done collapsing", q);
492
510 - /* We need to see which rename source really stays here;
511 - * earlier we only checked if the path is left in the result,
512 - * but even if a path remains in the result, if that is coming
513 - * from copying something else on top of it, then the original
514 - * source is lost and does not stay.
515 - */
516 - for (i = 0; i < q->nr; i++) {
517 - struct diff_filepair *p = q->queue[i];
518 - if (DIFF_PAIR_RENAME(p) && p->source_stays) {
519 - /* If one appears as the target of a rename-copy,
520 - * then mark p->source_stays = 0; otherwise
521 - * leave it as is.
522 - */
523 - p->source_stays = compute_stays(q, p->one);
524 - }
525 - }
526 -
493 for (i = 0; i < rename_dst_nr; i++)
494 free_filespec(rename_dst[i].two);
495
diffcore.h
+1 -1
@@ -31,6 +31,7 @@ struct diff_filespec {
31 unsigned long size;
32 int count; /* Reference count */
33 int xfrm_flags; /* for use by the xfrm */
34 + int rename_used; /* Count of rename users */
35 unsigned short mode; /* file mode */
36 unsigned sha1_valid : 1; /* if true, use sha1 and trust mode;
37 * if false, use the name and read from
@@ -58,7 +59,6 @@ struct diff_filepair {
59 struct diff_filespec *two;
60 unsigned short int score;
61 char status; /* M C R N D U (see Documentation/diff-format.txt) */
61 - unsigned source_stays : 1; /* all of R/C are copies */
62 unsigned broken_pair : 1;
63 unsigned renamed_pair : 1;
64 unsigned is_unmerged : 1;