Optimization of pure.base85.b85encode
authorMads Kiilerich <mads@kiilerich.com>
Wed, 04 Mar 2009 23:23:59 +0100
changeset 7835 2505e9f84153
parent 7825 6542be5df719
child 7836 bc0a87123ab8
Optimization of pure.base85.b85encode This makes pure python base85 encoding 3x faster than before. Now it is only 40x slower than the C version.
mercurial/pure/base85.py
--- a/mercurial/pure/base85.py	Wed Mar 04 19:04:21 2009 -0600
+++ b/mercurial/pure/base85.py	Wed Mar 04 23:23:59 2009 +0100
@@ -9,6 +9,7 @@
 
 _b85chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
             "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
+_b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
 _b85dec = {}
 
 def _mkb85dec():
@@ -22,24 +23,13 @@
     if r:
         text += '\0' * (4 - r)
     longs = len(text) >> 2
-    out = []
     words = struct.unpack('>%dL' % (longs), text)
-    for word in words:
-        # unrolling improved speed by 33%
-        word, r = divmod(word, 85)
-        e = _b85chars[r]
-        word, r = divmod(word, 85)
-        d = _b85chars[r]
-        word, r = divmod(word, 85)
-        c = _b85chars[r]
-        word, r = divmod(word, 85)
-        b = _b85chars[r]
-        word, r = divmod(word, 85)
-        a = _b85chars[r]
 
-        out += (a, b, c, d, e)
+    out = ''.join(_b85chars[(word / 52200625) % 85] + 
+                  _b85chars2[(word / 7225) % 7225] +
+                  _b85chars2[word % 7225]
+                  for word in words)
 
-    out = ''.join(out)
     if pad:
         return out