diffcore-rename: only compute dir_rename_count for relevant directories

When one side adds files to a directory that the other side renamed, directory rename detection is used to either move the new paths to the newer directory or warn the user about the fact that another path location might be better. If a parent of the given directory had new files added to it, any renames in the current directory are also part of determining where the parent directory is renamed to. Thus, naively, we need to record each rename N times for a path at depth N. However, we can use the additional information added to dirs_removed in the last commit to avoid traversing all N parent directories in many cases. Let's use an example to explain how this works. If we have a path named src/old_dir/a/b/file.c and src/old_dir doesn't exist on one side of history, but the other added a file named src/old_dir/newfile.c, then if one side renamed src/old_dir/a/b/file.c => source/new_dir/a/b/file.c then this file would affect potential directory rename detection counts for src/old_dir/a/b => source/new_dir/a/b src/old_dir/a => source/new_dir/a src/old_dir => source/new_dir src => source adding a weight of 1 to each in dir_rename_counts. However, if src/ exists on both sides of history, then we don't need to track any entries for it in dir_rename_counts. That was implemented previously. What we are adding now, is that if no new files were added to src/old_dir/a or src/old_dir/b, then we don't need to have counts in dir_rename_count for those directories either. In short, we only need to track counts in dir_rename_count for directories whose dirs_removed value is RELEVANT_FOR_SELF. And as soon as we reach a directory that isn't in dirs_removed (signalled by returning the default value of NOT_RELEVANT from strintmap_get()), we can stop looking any further up the directory hierarchy. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Mar 13, 2021 at 22:22 UTC e54385b97a69e09ec12be5f8eef234777c801257
1 file changed +22 -5
diffcore-rename.c
+22 -5
@@ -461,6 +461,8 @@ static void update_dir_rename_counts(struct dir_rename_info *info,
461 return;
462
463 while (1) {
464 + int drd_flag = NOT_RELEVANT;
465 +
466 /* Get old_dir, skip if its directory isn't relevant. */
467 dirname_munge(old_dir);
468 if (info->relevant_source_dirs &&
@@ -509,16 +511,31 @@ static void update_dir_rename_counts(struct dir_rename_info *info,
511 }
512 }
513
512 - if (strintmap_contains(dirs_removed, old_dir))
514 + /*
515 + * Above we suggested that we'd keep recording renames for
516 + * all ancestor directories where the trailing directories
517 + * matched, i.e. for
518 + * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
519 + * we'd increment rename counts for each of
520 + * a/b/c/d/e/ => a/b/some/thing/else/e/
521 + * a/b/c/d/ => a/b/some/thing/else/
522 + * However, we only need the rename counts for directories
523 + * in dirs_removed whose value is RELEVANT_FOR_SELF.
524 + * However, we add one special case of also recording it for
525 + * first_time_in_loop because find_basename_matches() can
526 + * use that as a hint to find a good pairing.
527 + */
528 + if (dirs_removed)
529 + drd_flag = strintmap_get(dirs_removed, old_dir);
530 + if (drd_flag == RELEVANT_FOR_SELF || first_time_in_loop)
531 increment_count(info, old_dir, new_dir);
514 - else
515 - break;
532
533 + first_time_in_loop = 0;
534 + if (drd_flag == NOT_RELEVANT)
535 + break;
536 /* If we hit toplevel directory ("") for old or new dir, quit */
537 if (!*old_dir || !*new_dir)
538 break;
520 -
521 - first_time_in_loop = 0;
539 }
540
541 /* Free resources we don't need anymore */