diffcore-rename: no point trying to find a match better than exact

diffcore_rename() had some code to avoid having destination paths that already had an exact rename detected from being re-checked for other renames. Source paths, however, were re-checked because we wanted to allow the possibility of detecting copies. But if copy detection isn't turned on, then this merely amounts to attempting to find a better-than-exact match, which naturally ends up being an expensive no-op. In particular, copy detection is never turned on by the merge machinery. For the testcases mentioned in commit 557ac0350d ("merge-ort: begin performance work; instrument with trace2_region_* calls", 2020-10-28), this change improves the performance as follows: Before After no-renames: 14.263 s ± 0.053 s 14.119 s ± 0.101 s mega-renames: 5504.231 s ± 5.150 s 1802.044 s ± 0.828 s just-one-mega: 158.534 s ± 0.498 s 51.391 s ± 0.028 s Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Feb 3, 2021 at 20:03 UTC f15eb7c1cf2674df69c1ff8aebdc5536a580c65e
1 file changed +14 -6
diffcore-rename.c
+14 -6
@@ -463,9 +463,11 @@ void diffcore_rename(struct diff_options *options)
463 struct diff_score *mx;
464 int i, j, rename_count, skip_unmodified = 0;
465 int num_destinations, dst_cnt;
466 + int num_sources, want_copies;
467 struct progress *progress = NULL;
468
469 trace2_region_enter("diff", "setup", options->repo);
470 + want_copies = (detect_rename == DIFF_DETECT_COPY);
471 if (!minimum_score)
472 minimum_score = DEFAULT_RENAME_SCORE;
473
@@ -502,7 +504,7 @@ void diffcore_rename(struct diff_options *options)
504 p->one->rename_used++;
505 register_rename_src(p);
506 }
505 - else if (detect_rename == DIFF_DETECT_COPY) {
507 + else if (want_copies) {
508 /*
509 * Increment the "rename_used" score by
510 * one, to indicate ourselves as a user.
@@ -532,12 +534,15 @@ void diffcore_rename(struct diff_options *options)
534 * files still remain as options for rename/copies!)
535 */
536 num_destinations = (rename_dst_nr - rename_count);
537 + num_sources = rename_src_nr;
538 + if (!want_copies)
539 + num_sources -= rename_count;
540
541 /* All done? */
537 - if (!num_destinations)
542 + if (!num_destinations || !num_sources)
543 goto cleanup;
544
540 - switch (too_many_rename_candidates(num_destinations, rename_src_nr,
545 + switch (too_many_rename_candidates(num_destinations, num_sources,
546 options)) {
547 case 1:
548 goto cleanup;
@@ -553,7 +558,7 @@ void diffcore_rename(struct diff_options *options)
558 if (options->show_rename_progress) {
559 progress = start_delayed_progress(
560 _("Performing inexact rename detection"),
556 - (uint64_t)num_destinations * (uint64_t)rename_src_nr);
561 + (uint64_t)num_destinations * (uint64_t)num_sources);
562 }
563
564 mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_destinations),
@@ -573,6 +578,9 @@ void diffcore_rename(struct diff_options *options)
578 struct diff_filespec *one = rename_src[j].p->one;
579 struct diff_score this_src;
580
581 + if (one->rename_used && !want_copies)
582 + continue;
583 +
584 if (skip_unmodified &&
585 diff_unmodified_pair(rename_src[j].p))
586 continue;
@@ -594,7 +602,7 @@ void diffcore_rename(struct diff_options *options)
602 }
603 dst_cnt++;
604 display_progress(progress,
597 - (uint64_t)dst_cnt * (uint64_t)rename_src_nr);
605 + (uint64_t)dst_cnt * (uint64_t)num_sources);
606 }
607 stop_progress(&progress);
608
@@ -602,7 +610,7 @@ void diffcore_rename(struct diff_options *options)
610 STABLE_QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);
611
612 rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
605 - if (detect_rename == DIFF_DETECT_COPY)
613 + if (want_copies)
614 rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
615 free(mx);
616 trace2_region_leave("diff", "inexact renames", options->repo);