[PATCH 3 of 3 v2] util.url: copy urllib.unquote() into util to improve startup times

Brodie Rao brodie at bitheap.org
Sat Apr 30 11:50:37 CDT 2011


# HG changeset patch
# User Brodie Rao <brodie at bitheap.org>
# Date 1304181803 25200
# Node ID 1bacbc417a26b969217f0fd1c0cf028c5f7a8acc
# Parent  cb2149c4cf63464dedd90a6a12144d4048576ed6
util.url: copy urllib.unquote() into util to improve startup times

The ui class uses util.hasscheme() in a couple of places, causing hg
to import urllib even when it doesn't need to. This copies
urllib.unquote() to avoid that import.

perfstartup time before the URL refactoring (8796fb6af67e):

! wall 0.050692 comb 0.000000 user 0.000000 sys 0.000000 (best of 100)

before this change:

! wall 0.064742 comb 0.000000 user 0.000000 sys 0.000000 (best of 100)

after this change:

! wall 0.052126 comb 0.000000 user 0.000000 sys 0.000000 (best of 100

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1284,6 +1284,26 @@ def parsebool(s):
     """
     return _booleans.get(s.lower(), None)
 
+_hexdig = '0123456789ABCDEFabcdef'
+_hextochr = dict((a + b, chr(int(a + b, 16)))
+                 for a in _hexdig for b in _hexdig)
+
+def _urlunquote(s):
+    """unquote('abc%20def') -> 'abc def'."""
+    res = s.split('%')
+    # fastpath
+    if len(res) == 1:
+        return s
+    s = res[0]
+    for item in res[1:]:
+        try:
+            s += _hextochr[item[:2]] + item[2:]
+        except KeyError:
+            s += '%' + item
+        except UnicodeDecodeError:
+            s += unichr(int(item[:2], 16)) + item[2:]
+    return s
+
 class url(object):
     """Reliable URL parser.
 
@@ -1427,7 +1447,7 @@ class url(object):
                   'path', 'query', 'fragment'):
             v = getattr(self, a)
             if v is not None:
-                setattr(self, a, urllib.unquote(v))
+                setattr(self, a, _urlunquote(v))
 
     def __repr__(self):
         attrs = []


More information about the Mercurial-devel mailing list