Patchwork [3,of,5,rfc] bdiff: rearrange the better longest match code

login
register
mail settings
Submitter Mads Kiilerich
Date Nov. 3, 2016, 9:34 p.m.
Message ID <a3e3c7075c3c4b92e6b8.1478208853@madski>
Download mbox | patch
Permalink /patch/17324/
State Deferred
Headers show

Comments

Mads Kiilerich - Nov. 3, 2016, 9:34 p.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1478208837 -3600
#      Thu Nov 03 22:33:57 2016 +0100
# Node ID a3e3c7075c3c4b92e6b8c27e28bef7b2c061008d
# Parent  c593308da04e9144da01a08401d886a64985c74b
bdiff: rearrange the better longest match code

Primarily to make the code more managable and prepare for later changes.

More specific assignments might also be slightly faster, even thought it also
might generate a bit more code.

Patch

diff --git a/mercurial/bdiff.c b/mercurial/bdiff.c
--- a/mercurial/bdiff.c
+++ b/mercurial/bdiff.c
@@ -172,10 +172,20 @@  static int longest_match(struct bdiff_li
 
 			/* best match so far? we prefer matches closer
 			   to the middle to balance recursion */
-			if (k > mk || (k == mk && (i <= mi || i <= half))) {
+			if (k > mk) {
+				/* a longer match is always better */
 				mi = i;
 				mj = j;
 				mk = k;
+			} else if (k == mk) {
+				if (i > mi && i <= half) {
+					/* better i in first lower half */
+					mi = i;
+					mj = j;
+				} else if (i == mi) {
+					/* an earlier j is "better" */
+					mj = j;
+				}
 			}
 		}
 	}