util.url: copy urllib.unquote() into util to improve startup times
authorBrodie Rao <brodie@bitheap.org>
Sat, 30 Apr 2011 09:43:23 -0700
changeset 14077 c285bdb0572a
parent 14076 924c82157d46
child 14078 80b8c0b59178
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
mercurial/util.py
--- a/mercurial/util.py	Sat Apr 30 09:43:20 2011 -0700
+++ b/mercurial/util.py	Sat Apr 30 09:43:23 2011 -0700
@@ -1284,6 +1284,26 @@
     """
     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 @@
                   '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 = []