Chris Mason mason at suse.com
Mon Aug 22 12:44:41 CDT 2005

```# HG changeset patch
# User mason at suse.com

the delta found in the main manifest.add loop, this patch just pulls
the compression out into a helper function, and the result of the
compression is sent to both addlistdelta and gendelta.

This patch turns:

Into

addlist[1:4] = ["new line1", "new line2", "new line3"]

Performance gain is only 3-4% in the common case, but it is much cleaner.

Index: mine/mercurial/hg.py
===================================================================
--- mine.orig/mercurial/hg.py	2005-08-22 13:18:24.000000000 -0400
+++ mine/mercurial/hg.py	2005-08-22 13:19:41.000000000 -0400
@@ -145,23 +145,34 @@ class manifest(revlog):
changed=None):
# directly generate the mdiff delta from the data collected during
# the bisect loop below
-        def gendelta(delta):
+            result = []
+            for x in delta:
+                if x[2]:
+                    s = "".join(x[2])
+                else:
+                    s = ""
+                result.append(struct.pack(">lll", start, end, len(s)) + s)
+            return result
+        def compressdelta(delta):
i = 0
result = []
while i < len(delta):
-                start = delta[i][2]
-                end = delta[i][3]
-                l = delta[i][4]
-                if l == None:
-                    l = ""
-                while i < len(delta) - 1 and start <= delta[i+1][2] \
-                          and end >= delta[i+1][2]:
-                    if delta[i+1][3] > end:
-                        end = delta[i+1][3]
-                    if delta[i+1][4]:
-                        l += delta[i+1][4]
+                start = delta[i][0]
+                end = delta[i][1]
+                l = []
+                if delta[i][2]:
+                    l.append(delta[i][2])
+                while i < (len(delta) - 1) and start <= delta[i+1][0] \
+                          and end >= delta[i+1][0]:
+                    if delta[i+1][1] > end:
+                        end = delta[i+1][1]
+                    if delta[i+1][2]:
+                        l.append(delta[i+1][2])
i += 1
-                result.append(struct.pack(">lll", start, end, len(l)) +  l)
+                result.append([start, end, l])
i += 1
return result

@@ -174,8 +185,8 @@ class manifest(revlog):
i -= 1
start = delta[i][0]
end = delta[i][1]
-                if delta[i][4]:
+                if delta[i][2]:
else:
@@ -229,13 +240,14 @@ class manifest(revlog):
# item is found, replace/delete the existing line
start = bs - 1
end = bs
+                delta.append([start, end, l])

+            cdelta = compressdelta(delta)
if self.mapcache[0] == self.tip():
-                cachedelta = "".join(gendelta(delta))