Make compression more intelligent:
authormpm@selenic.com
Fri, 20 May 2005 17:31:12 -0800
changeset 112 aea6562add6c
parent 111 2c80f6f8fc08
child 113 1918852a67a8
Make compression more intelligent: - we don't attempt to compress things under 44 bytes (empirical) - we check whether larger objects actually compress - we tag objects to indicate their compression NUL means uncompressed and starts with NUL x means gzipped and starts with x (handy) u means uncompressed, drop the u
mercurial/revlog.py
--- a/mercurial/revlog.py	Fri May 20 17:28:09 2005 -0800
+++ b/mercurial/revlog.py	Fri May 20 17:31:12 2005 -0800
@@ -16,10 +16,23 @@
 def short(node): return hex(node[:4])
 
 def compress(text):
-    return zlib.compress(text)
+    if not text: return text
+    if len(text) < 44:
+        if text[0] == '\0': return text
+        return 'u' + text
+    bin = zlib.compress(text)
+    if len(bin) > len(text):
+        if text[0] == '\0': return text
+        return 'u' + text
+    return bin
 
 def decompress(bin):
-    return zlib.decompress(bin)
+    if not bin: return bin
+    t = bin[0]
+    if t == '\0': return bin
+    if t == 'x': return zlib.decompress(bin)
+    if t == 'u': return bin[1:]
+    raise "unknown compression type %s" % t
 
 def hash(text, p1, p2):
     l = [p1, p2]