changeset 7835:2505e9f84153

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.
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 04 Mar 2009 23:23:59 +0100
parents 6542be5df719
children bc0a87123ab8
files mercurial/pure/base85.py
diffstat 1 files changed, 5 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- 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