[pypy-commit] pypy default: small change for string's repr to make common visible characters (>= 32 and < 128) require less checks. speeds up by about 20% for strings with such characters.

justinpeel noreply at buildbot.pypy.org
Sat Aug 20 07:13:23 CEST 2011


Author: Justin Peel <notmuchtotell at gmail.com>
Branch: 
Changeset: r46653:1f8c162174c6
Date: 2011-08-19 23:17 -0600
http://bitbucket.org/pypy/pypy/changeset/1f8c162174c6/

Log:	small change for string's repr to make common visible characters (>=
	32 and < 128) require less checks. speeds up by about 20% for
	strings with such characters.

diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -924,35 +924,36 @@
 
     for i in range(len(s)):
         c = s[i]
-        use_bs_char = False # character quoted by backspace
+        if not '\x20' <= c < '\x7f':
+            use_bs_char = False # character quoted by backspace
 
-        if c == '\\' or c == quote:
-            bs_char = c
-            use_bs_char = True
-        elif c == '\t':
-            bs_char = 't'
-            use_bs_char = True
-        elif c == '\r':
-            bs_char = 'r'
-            use_bs_char = True
-        elif c == '\n':
-            bs_char = 'n'
-            use_bs_char = True
-        elif not '\x20' <= c < '\x7f':
-            n = ord(c)
-            if i != startslice:
-                buf.append_slice(s, startslice, i)
-            startslice = i + 1
-            buf.append('\\x')
-            buf.append("0123456789abcdef"[n>>4])
-            buf.append("0123456789abcdef"[n&0xF])
+            if c == '\\' or c == quote:
+                bs_char = c
+                use_bs_char = True
+            elif c == '\t':
+                bs_char = 't'
+                use_bs_char = True
+            elif c == '\r':
+                bs_char = 'r'
+                use_bs_char = True
+            elif c == '\n':
+                bs_char = 'n'
+                use_bs_char = True
+            else:
+                n = ord(c)
+                if i != startslice:
+                    buf.append_slice(s, startslice, i)
+                startslice = i + 1
+                buf.append('\\x')
+                buf.append("0123456789abcdef"[n>>4])
+                buf.append("0123456789abcdef"[n&0xF])
 
-        if use_bs_char:
-            if i != startslice:
-                buf.append_slice(s, startslice, i)
-            startslice = i + 1
-            buf.append('\\')
-            buf.append(bs_char)
+            if use_bs_char:
+                if i != startslice:
+                    buf.append_slice(s, startslice, i)
+                startslice = i + 1
+                buf.append('\\')
+                buf.append(bs_char)
 
     if len(s) != startslice:
         buf.append_slice(s, startslice, len(s))


More information about the pypy-commit mailing list