Patchwork [4,of,7,v2] bdiff: rearrange the "better longest match" code

login
register
mail settings
Submitter Mads Kiilerich
Date Nov. 15, 2016, 8:57 p.m.
Message ID <5bb26f29b1509520ca3a.1479243425@madski>
Download mbox | patch
Permalink /patch/17591/
State Accepted
Headers show

Comments

Mads Kiilerich - Nov. 15, 2016, 8:57 p.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1478626653 -3600
#      Tue Nov 08 18:37:33 2016 +0100
# Node ID 5bb26f29b1509520ca3af4c540775cab50b4d6c0
# Parent  fbce612c9be26860338f6ba4ab5df819b6d79521
bdiff: rearrange the "better longest match" code

This is 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
@@ -177,10 +177,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 */
 				mi = i;
 				mj = j;
 				mk = k;
+			} else if (k == mk) {
+				if (i > mi && i <= half) {
+					/* same match but closer to half */
+					mi = i;
+					mj = j;
+				} else if (i == mi) {
+					/* same i but earlier j */
+					mj = j;
+				}
 			}
 		}
 	}