@matheusbsbs / matheus-bagliano / commits / aec8fa1f58
Fork 0

git-pickaxe: swap comparison loop used for -C

When assigning blames for code movements across file boundaries, we used to iterate over blame entries (i.e. groups of lines to be blamed) in the outer loop and compared each entry with paths in the parent commit in an inner loop. This meant that we opened the blob data from each path number of times. Reorganize the loop so that we read the same path only once, and compare it against all relevant blame entries. This should perform better, but seems to give mixed results, though. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 21, 2006 at 03:30 UTC aec8fa1f587d68a0e50ada2720c8dc6e09709a9c
1 file changed +50 -41
builtin-pickaxe.c
+50 -41
@@ -737,11 +737,27 @@ static int find_copy_in_parent(struct scoreboard *sb,
737 struct diff_options diff_opts;
738 const char *paths[1];
739 struct blame_entry *e;
740 - int i;
740 + int i, j;
741 + struct blame_list {
742 + struct blame_entry *ent;
743 + struct blame_entry split[3];
744 + } *blame_list;
745 + int num_ents;
746
742 - if (find_last_in_target(sb, target) < 0)
747 + /* Count the number of entries the target is suspected for,
748 + * and prepare a list of entry and the best split.
749 + */
750 + for (e = sb->ent, num_ents = 0; e; e = e->next)
751 + if (!e->guilty && !cmp_suspect(e->suspect, target))
752 + num_ents++;
753 + if (!num_ents)
754 return 1; /* nothing remains for this target */
755
756 + blame_list = xcalloc(num_ents, sizeof(struct blame_list));
757 + for (e = sb->ent, i = 0; e; e = e->next)
758 + if (!e->guilty && !cmp_suspect(e->suspect, target))
759 + blame_list[i++].ent = e;
760 +
761 diff_setup(&diff_opts);
762 diff_opts.recursive = 1;
763 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
@@ -761,52 +777,45 @@ static int find_copy_in_parent(struct scoreboard *sb,
777 "", &diff_opts);
778 diffcore_std(&diff_opts);
779
764 -
765 - /*
766 - * NEEDSWORK: This loop is wasteful in that it opens the same
767 - * blob in the parent number of times. We should swap the
768 - * loop inside out, which would require keeping track of
769 - * "best blame so far" for blame entries that the current
770 - * "target" is being suspected.
771 - */
772 -
773 - for (e = sb->ent; e; e = e->next) {
774 - struct blame_entry split[3];
775 - if (e->guilty || cmp_suspect(e->suspect, target))
780 + for (i = 0; i < diff_queued_diff.nr; i++) {
781 + struct diff_filepair *p = diff_queued_diff.queue[i];
782 + struct origin *norigin;
783 + mmfile_t file_p;
784 + char type[10];
785 + char *blob;
786 + struct blame_entry this[3];
787 +
788 + if (!DIFF_FILE_VALID(p->one))
789 + continue; /* does not exist in parent */
790 + if (porigin && !strcmp(p->one->path, porigin->path))
791 + /* find_move already dealt with this path */
792 continue;
793
778 - memset(split, 0, sizeof(split));
779 - for (i = 0; i < diff_queued_diff.nr; i++) {
780 - struct diff_filepair *p = diff_queued_diff.queue[i];
781 - struct origin *norigin;
782 - mmfile_t file_p;
783 - char type[10];
784 - char *blob;
785 - struct blame_entry this[3];
794 + norigin = get_origin(sb, parent, p->one->path);
795 + hashcpy(norigin->blob_sha1, p->one->sha1);
796 + blob = read_sha1_file(norigin->blob_sha1, type,
797 + (unsigned long *) &file_p.size);
798 + file_p.ptr = blob;
799 + if (!file_p.ptr)
800 + continue;
801
787 - if (!DIFF_FILE_VALID(p->one))
788 - continue; /* does not exist in parent */
789 - if (porigin && !strcmp(p->one->path, porigin->path))
790 - /* find_move already dealt with this path */
791 - continue;
792 - norigin = get_origin(sb, parent, p->one->path);
793 - hashcpy(norigin->blob_sha1, p->one->sha1);
794 - blob = read_sha1_file(norigin->blob_sha1, type,
795 - (unsigned long *) &file_p.size);
796 - file_p.ptr = blob;
797 - if (!file_p.ptr) {
798 - free(blob);
799 - continue;
800 - }
801 - find_copy_in_blob(sb, e, norigin, this, &file_p);
802 - copy_split_if_better(sb, split, this);
803 - free(blob);
802 + for (j = 0; j < num_ents; j++) {
803 + find_copy_in_blob(sb, blame_list[j].ent, norigin,
804 + this, &file_p);
805 + copy_split_if_better(sb, blame_list[j].split,
806 + this);
807 }
808 + free(blob);
809 + }
810 + diff_flush(&diff_opts);
811 +
812 + for (j = 0; j < num_ents; j++) {
813 + struct blame_entry *split = blame_list[j].split;
814 if (split[1].suspect &&
815 blame_copy_score < ent_score(sb, &split[1]))
807 - split_blame(sb, split, e);
816 + split_blame(sb, split, blame_list[j].ent);
817 }
809 - diff_flush(&diff_opts);
818 + free(blame_list);
819
820 return 0;
821 }