[Python-checkins] cpython: Speed up IDNA for the common case

antoine.pitrou python-checkins at python.org
Thu Nov 10 22:53:52 CET 2011


http://hg.python.org/cpython/rev/6e1071ed4c66
changeset:   73488:6e1071ed4c66
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Thu Nov 10 22:49:20 2011 +0100
summary:
  Speed up IDNA for the common case

files:
  Lib/encodings/idna.py |  22 ++++++++++++++++++++++
  1 files changed, 22 insertions(+), 0 deletions(-)


diff --git a/Lib/encodings/idna.py b/Lib/encodings/idna.py
--- a/Lib/encodings/idna.py
+++ b/Lib/encodings/idna.py
@@ -153,6 +153,20 @@
         if not input:
             return b'', 0
 
+        try:
+            result = input.encode('ascii')
+        except UnicodeEncodeError:
+            pass
+        else:
+            # ASCII name: fast path
+            labels = result.split(b'.')
+            for label in labels[:-1]:
+                if not (0 < len(label) < 64):
+                    raise UnicodeError("label empty or too long")
+            if len(labels[-1]) >= 64:
+                raise UnicodeError("label too long")
+            return result, len(input)
+
         result = bytearray()
         labels = dots.split(input)
         if labels and not labels[-1]:
@@ -179,6 +193,14 @@
         if not isinstance(input, bytes):
             # XXX obviously wrong, see #3232
             input = bytes(input)
+
+        if ace_prefix not in input:
+            # Fast path
+            try:
+                return input.decode('ascii'), len(input)
+            except UnicodeDecodeError:
+                pass
+
         labels = input.split(b".")
 
         if labels and len(labels[-1]) == 0:

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list