Skip to content

Commit 301ef85

Browse files
stefanbellergitster
authored andcommitted
xdiff: reduce indent heuristic overhead
Skip searching for better indentation heuristics if we'd slide a hunk more than its size. This is the easiest fix proposed in the analysis[1] in response to a patch that mercurial took for xdiff to limit searching by a constant. Using a performance test as: #!python open('a', 'w').write(" \n" * 1000000) open('b', 'w').write(" \n" * 1000001) This patch reduces the execution of "git diff --no-index a b" from 0.70s to 0.31s. However limiting the sliding to the size of the diff hunk, which was proposed as a solution (that I found easiest to implement for now) is not optimal for cases like open('a', 'w').write(" \n" * 1000000) open('b', 'w').write(" \n" * 2000000) as then we'd still slide 1000000 times. In addition to limiting the sliding to size of the hunk, also limit by a constant. Choose 100 lines as the constant as that fits more than a screen, which really means that the diff sliding is probably not providing a lot of benefit anyway. [1] https://public-inbox.org/git/[email protected]/ Reported-by: Jun Wu <[email protected]> Analysis-by: Michael Haggerty <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 53f9a3e commit 301ef85

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

xdiff/xdiffi.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,11 @@ static void measure_split(const xdfile_t *xdf, long split,
591591
*/
592592
#define INDENT_WEIGHT 60
593593

594+
/*
595+
* How far do we slide a hunk at most?
596+
*/
597+
#define INDENT_HEURISTIC_MAX_SLIDING 100
598+
594599
/*
595600
* Compute a badness score for the hypothetical split whose measurements are
596601
* stored in m. The weight factors were determined empirically using the tools and
@@ -903,7 +908,12 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
903908
long shift, best_shift = -1;
904909
struct split_score best_score;
905910

906-
for (shift = earliest_end; shift <= g.end; shift++) {
911+
shift = earliest_end;
912+
if (g.end - groupsize - 1 > shift)
913+
shift = g.end - groupsize - 1;
914+
if (g.end - INDENT_HEURISTIC_MAX_SLIDING > shift)
915+
shift = g.end - INDENT_HEURISTIC_MAX_SLIDING;
916+
for (; shift <= g.end; shift++) {
907917
struct split_measurement m;
908918
struct split_score score = {0, 0};
909919

0 commit comments

Comments
 (0)