[PATCH 1 of 4] revlog: move compress method outside revlog class
Wojciech Lopata
lopek at fb.com
Thu Sep 26 13:14:42 CDT 2013
# HG changeset patch
# User Wojciech Lopata <lopek at fb.com>
# Date 1380209082 25200
# Thu Sep 26 08:24:42 2013 -0700
# Node ID 54ae55d8365d44a5bc8404311531f380c9fd5ad5
# Parent c80feeb715d1ebe9232c00881d43c1bbb594b3fa
revlog: move compress method outside revlog class
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -75,6 +75,35 @@
s.update(text)
return s.digest()
+def compress(text):
+ """ generate a possibly-compressed representation of text """
+ if not text:
+ return ("", text)
+ l = len(text)
+ bin = None
+ if l < 44:
+ pass
+ elif l > 1000000:
+ # zlib makes an internal copy, thus doubling memory usage for
+ # large files, so lets do this in pieces
+ z = zlib.compressobj()
+ p = []
+ pos = 0
+ while pos < l:
+ pos2 = pos + 2**20
+ p.append(z.compress(text[pos:pos2]))
+ pos = pos2
+ p.append(z.flush())
+ if sum(map(len, p)) < l:
+ bin = "".join(p)
+ else:
+ bin = _compress(text)
+ if bin is None or len(bin) > l:
+ if text[0] == '\0':
+ return ("", text)
+ return ('u', text)
+ return ("", bin)
+
def decompress(bin):
""" decompress the given input """
if not bin:
@@ -1053,35 +1082,6 @@
dfh.close()
ifh.close()
- def compress(self, text):
- """ generate a possibly-compressed representation of text """
- if not text:
- return ("", text)
- l = len(text)
- bin = None
- if l < 44:
- pass
- elif l > 1000000:
- # zlib makes an internal copy, thus doubling memory usage for
- # large files, so lets do this in pieces
- z = zlib.compressobj()
- p = []
- pos = 0
- while pos < l:
- pos2 = pos + 2**20
- p.append(z.compress(text[pos:pos2]))
- pos = pos2
- p.append(z.flush())
- if sum(map(len, p)) < l:
- bin = "".join(p)
- else:
- bin = _compress(text)
- if bin is None or len(bin) > l:
- if text[0] == '\0':
- return ("", text)
- return ('u', text)
- return ("", bin)
-
def _addrevision(self, node, text, transaction, link, p1, p2,
cachedelta, ifh, dfh):
"""internal function to add revisions to the log
@@ -1112,7 +1112,7 @@
t = buildtext()
ptext = self.revision(self.node(rev))
delta = mdiff.textdiff(ptext, t)
- data = self.compress(delta)
+ data = compress(delta)
l = len(data[1]) + len(data[0])
if basecache[0] == rev:
chainbase = basecache[1]
@@ -1158,7 +1158,7 @@
textlen = len(text)
if d is None or dist > textlen * 2:
text = buildtext()
- data = self.compress(text)
+ data = compress(text)
l = len(data[1]) + len(data[0])
base = chainbase = curr
More information about the Mercurial-devel
mailing list