xdiff-interface trims common suffix if ctxlen is 0. Teach it to also
trim common prefix, and trim less lines if ctxlen > 0. So it can benefit
the default diff command, as seen by profiling:

  $ GIT_PERF_REPEAT_COUNT=10 ./run origin/master . -- p4000-diff-algorithms.sh
  [...]
  Test                                  origin/master     this tree
  ------------------------------------------------------------------------------
  4000.1: log -3000 (baseline)          0.07(0.06+0.01)   0.06(0.05+0.01) -14.3%
  4000.2: log --raw -3000 (tree-only)   0.31(0.28+0.02)   0.31(0.27+0.02) +0.0%
  4000.3: log -p -3000 (Myers)          2.25(2.05+0.17)   1.66(1.52+0.11) -26.2%
  4000.4: log -p -3000 --histogram      2.51(2.36+0.10)   1.90(1.79+0.09) -24.3%
  4000.5: log -p -3000 --patience       2.63(2.44+0.16)   1.70(1.56+0.12) -35.4%

Diff shifting behaviors (hunk merging, indent heuristic, shift towards
the end for repetitive content) are preserved as a best effort by:

  1. Reserve extra 100 lines to not be trimmed. Leave room for shifting.
  2. Calculate common prefix first. Disallow suffix overlap with prefix
     in the smaller file. So diff hunk tends to be shifted down.

Add tests for those. Make sure they fail on the old code.

Backported from a Mercurial patch [1]. Adjusted to git's existing
xdiff-interface.c (which isn't ported to Mercurial).

Note: xdl_trim_ends does a similar job. But too late - it's after line
splitting, hashing, correcting hash values. Those steps have visible
overhead.

[1]: https://phab.mercurial-scm.org/D2686

Signed-off-by: Jun Wu <qu...@fb.com>
---
 t/t4066-diff-trimming.sh | 49 +++++++++++++++++++++++++++++++++++++++
 xdiff-interface.c        | 60 +++++++++++++++++++++++++++++++++---------------
 xdiff/xdiff.h            |  3 +++
 xdiff/xdiffi.c           |  5 ++--
 xdiff/xemit.c            |  3 ++-
 5 files changed, 98 insertions(+), 22 deletions(-)
 create mode 100755 t/t4066-diff-trimming.sh

diff --git a/t/t4066-diff-trimming.sh b/t/t4066-diff-trimming.sh
new file mode 100755
index 000000000..3d90175ac
--- /dev/null
+++ b/t/t4066-diff-trimming.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+test_description='diff trimming optimization'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+  printf "x\n%.0s" {1..1000} >a &&
+  printf "x\n%.0s" {1..1001} >b &&
+  cat >c <<EOF1 && cat >d <<EOF2 &&\
+  printf "x%.0s" {1..934} >>c &&\
+  printf "x%.0s" {1..934} >>d # pad common suffix to 1024 bytes
+  try:
+      import foo
+  except ImportError:
+      pass
+  try:
+      import bar
+  except ImportError:
+      pass
+EOF1
+  try:
+      import foo
+  except ImportError:
+      pass
+  try:
+      import baz
+  except ImportError:
+      pass
+  try:
+      import bar
+  except ImportError:
+      pass
+EOF2
+'
+
+test_expect_success 'git diff -U0 shifts hunk towards the end' '
+       test_expect_code 1 git diff -U0 --no-index a b |\
+    fgrep "@@ -1000,0 +1001 @@" &&
+       test_expect_code 1 git diff -U0 --no-index b a |\
+    fgrep "@@ -1001 +1000,0 @@"
+'
+
+test_expect_success 'git diff -U0 --indent-heuristic' '
+       test_expect_code 1 git diff -U0 --no-index --indent-heuristic c d |\
+    fgrep "@@ -4,0 +5,4 @@"
+'
+
+test_done
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 770e1f7f8..a4141e2db 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -101,42 +101,64 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int 
nbuf)
 }
 
 /*
- * Trim down common substring at the end of the buffers,
- * but end on a complete line.
+ * Trim down common prefix and suffix, except for reserved lines.
+ * Align to line boundary.
  */
-static void trim_common_tail(mmfile_t *a, mmfile_t *b)
+static void trim_common(mmfile_t *a, mmfile_t *b, long reserved,
+                       xdemitconf_t *xecfg)
 {
        const int blk = 1024;
-       long trimmed = 0, recovered = 0;
-       char *ap = a->ptr + a->size;
-       char *bp = b->ptr + b->size;
+       long i = 0, lines = 0;
+       char *ap = a->ptr, *ae = a->ptr + a->size - 1;
+       char *bp = b->ptr, *be = b->ptr + b->size - 1;
        long smaller = (a->size < b->size) ? a->size : b->size;
+       size_t prefix = 0, suffix = 0;
 
-       while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
-               trimmed += blk;
-               ap -= blk;
-               bp -= blk;
+       if (smaller == 0)
+               return;
+
+       /* prefix - need line count for xecfg->ptrimmed */
+       for (i = 0; ++i < smaller && *ap == *bp;) {
+               lines += (*ap == '\n');
+               ap++, bp++;
+       }
+       for (i = 0; i <= reserved && --ap >= a->ptr;)
+               i += (*ap == '\n');
+       if (ap > a->ptr) {
+               prefix = ap + 1 - a->ptr;
+               xecfg->ptrimmed = lines + 1 - i;
        }
 
-       while (recovered < trimmed)
-               if (ap[recovered++] == '\n')
-                       break;
-       a->size -= trimmed - recovered;
-       b->size -= trimmed - recovered;
-}
+       /* suffix - no line count, but do not overlap with prefix */
+       for (i = bp - b->ptr; (i += blk) < smaller && !memcmp(ae - blk, be - 
blk, blk);)
+               ae -= blk, be -= blk;
+       for (i = 0; i <= reserved && ++ae < a->ptr + a->size;)
+               i += (*ae == '\n');
+       if (ae < a->ptr + a->size)
+               suffix = a->ptr + a->size - 1 - ae;
+
+       a->size -= prefix + suffix;
+       b->size -= prefix + suffix;
+       a->ptr += prefix;
+       b->ptr += prefix;
+};
 
 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t 
const *xecfg, xdemitcb_t *xecb)
 {
        mmfile_t a = *mf1;
        mmfile_t b = *mf2;
+       xdemitconf_t ecfg = *xecfg;
 
        if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
                return -1;
 
-       if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
-               trim_common_tail(&a, &b);
+       if (!(xecfg->flags & XDL_EMIT_FUNCCONTEXT)) {
+               /* reserve 100 lines for hunk shifting */
+               long reserved = xecfg->ctxlen + 100;
+               trim_common(&a, &b, reserved, &ecfg);
+       }
 
-       return xdl_diff(&a, &b, xpp, xecfg, xecb);
+       return xdl_diff(&a, &b, xpp, &ecfg, xecb);
 }
 
 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
diff --git a/xdiff/xdiff.h b/xdiff/xdiff.h
index c1937a291..737d96607 100644
--- a/xdiff/xdiff.h
+++ b/xdiff/xdiff.h
@@ -110,6 +110,9 @@ typedef struct s_xdemitconf {
        find_func_t find_func;
        void *find_func_priv;
        xdl_emit_hunk_consume_func_t hunk_func;
+
+       /* prefix lines trimmed */
+       long ptrimmed;
 } xdemitconf_t;
 
 typedef struct s_bdiffparam {
diff --git a/xdiff/xdiffi.c b/xdiff/xdiffi.c
index 0de1ef463..d62c5769e 100644
--- a/xdiff/xdiffi.c
+++ b/xdiff/xdiffi.c
@@ -981,13 +981,14 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t 
*xscr, xdemitcb_t *ecb,
                              xdemitconf_t const *xecfg)
 {
        xdchange_t *xch, *xche;
+       long p = xecfg->ptrimmed;
 
        for (xch = xscr; xch; xch = xche->next) {
                xche = xdl_get_hunk(&xch, xecfg);
                if (!xch)
                        break;
-               if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1,
-                                    xch->i2, xche->i2 + xche->chg2 - xch->i2,
+               if (xecfg->hunk_func(xch->i1 + p, xche->i1 + xche->chg1 - 
xch->i1,
+                                    xch->i2 + p, xche->i2 + xche->chg2 - 
xch->i2,
                                     ecb->priv) < 0)
                        return -1;
        }
diff --git a/xdiff/xemit.c b/xdiff/xemit.c
index 7778dc2b1..45d4ab073 100644
--- a/xdiff/xemit.c
+++ b/xdiff/xemit.c
@@ -167,6 +167,7 @@ static int is_empty_rec(xdfile_t *xdf, long ri)
 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
                  xdemitconf_t const *xecfg) {
        long s1, s2, e1, e2, lctx;
+       long p = xecfg->ptrimmed + 1;
        xdchange_t *xch, *xche;
        long funclineprev = -1;
        struct func_line func_line = { 0 };
@@ -261,7 +262,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, 
xdemitcb_t *ecb,
                                      s1 - 1, funclineprev);
                        funclineprev = s1 - 1;
                }
-               if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
+               if (xdl_emit_hunk_hdr(s1 + p, e1 - s1, s2 + p, e2 - s2,
                                      func_line.buf, func_line.len, ecb) < 0)
                        return -1;
 
-- 
2.16.2


Reply via email to