diff --color-moved: shrink potential moved blocks as we go

Rather than setting `match` to NULL and then looping over the list of potential matched blocks for a second time to remove blocks with no matches just filter out the blocks with no matches as we go. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Dec 9, 2021 at 10:30 UTC 0e488f173257e5ab766267ac5a5c4c9f3f1ce343
1 file changed +8 -36
diff.c
+8 -36
@@ -996,12 +996,12 @@ static void add_lines_to_move_detection(struct diff_options *o,
996 static void pmb_advance_or_null(struct diff_options *o,
997 struct emitted_diff_symbol *l,
998 struct moved_block *pmb,
999 - int pmb_nr)
999 + int *pmb_nr)
1000 {
1001 - int i;
1001 + int i, j;
1002 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
1003
1004 - for (i = 0; i < pmb_nr; i++) {
1004 + for (i = 0, j = 0; i < *pmb_nr; i++) {
1005 int match;
1006 struct moved_entry *prev = pmb[i].match;
1007 struct moved_entry *cur = (prev && prev->next_line) ?
@@ -1015,38 +1015,12 @@ static void pmb_advance_or_null(struct diff_options *o,
1015 match = cur &&
1016 xdiff_compare_lines(cur->es->line, cur->es->len,
1017 l->line, l->len, flags);
1018 - if (match)
1019 - pmb[i].match = cur;
1020 - else
1021 - moved_block_clear(&pmb[i]);
1022 - }
1023 -}
1024 -
1025 -static int shrink_potential_moved_blocks(struct moved_block *pmb,
1026 - int pmb_nr)
1027 -{
1028 - int lp, rp;
1029 -
1030 - /* Shrink the set of potential block to the remaining running */
1031 - for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
1032 - while (lp < pmb_nr && pmb[lp].match)
1033 - lp++;
1034 - /* lp points at the first NULL now */
1035 -
1036 - while (rp > -1 && !pmb[rp].match)
1037 - rp--;
1038 - /* rp points at the last non-NULL */
1039 -
1040 - if (lp < pmb_nr && rp > -1 && lp < rp) {
1041 - pmb[lp] = pmb[rp];
1042 - memset(&pmb[rp], 0, sizeof(pmb[rp]));
1043 - rp--;
1044 - lp++;
1018 + if (match) {
1019 + pmb[j] = pmb[i];
1020 + pmb[j++].match = cur;
1021 }
1022 }
1047 -
1048 - /* Remember the number of running sets */
1049 - return rp + 1;
1023 + *pmb_nr = j;
1024 }
1025
1026 static void fill_potential_moved_blocks(struct diff_options *o,
@@ -1181,9 +1155,7 @@ static void mark_color_as_moved(struct diff_options *o,
1155 continue;
1156 }
1157
1184 - pmb_advance_or_null(o, l, pmb, pmb_nr);
1185 -
1186 - pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
1158 + pmb_advance_or_null(o, l, pmb, &pmb_nr);
1159
1160 if (pmb_nr == 0) {
1161 int contiguous = adjust_last_block(o, n, block_length);