[pypy-svn] r23126 - pypy/dist/pypy/objspace/std

xoraxax at codespeak.net xoraxax at codespeak.net
Tue Feb 7 21:38:56 CET 2006


Author: xoraxax
Date: Tue Feb  7 21:38:54 2006
New Revision: 23126

Modified:
   pypy/dist/pypy/objspace/std/stringobject.py
Log:
Optimised repr__String (avoiding string concatenation). 
Given a 1000 bytes string, this speeds it up by the factor ~ 800x, compared to ~ 300x of the same implementation in RPython (before, it ran on app-level).

Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Tue Feb  7 21:38:54 2006
@@ -926,6 +926,49 @@
 def getnewargs__String(space, w_str):
     return space.newtuple([W_StringObject(space, w_str._value)])
 
+def repr__String(space, w_str):
+    s = w_str._value
+
+    i = 0
+    buf = [' '] * (len(s) * 4 + 2) # safely overallocate
+
+    quote = "'"
+    if quote in s and '"' not in s:
+        quote = '"'
+
+    buf[i] = quote
+
+    for c in s:
+        i += 1
+        bs_char = None # character quoted by backspace
+
+        if c == '\\' or c == quote:
+            bs_char = c
+        elif c == '\t': bs_char = 't'
+        elif c == '\r': bs_char = 'r'
+        elif c == '\n': bs_char = 'n'
+        elif not '\x20' <= c < '\x7f':
+            n = ord(c)
+            buf[i] = '\\'
+            i += 1
+            buf[i] = 'x'
+            i += 1
+            buf[i] = "0123456789abcdef"[n>>4]
+            i += 1
+            buf[i] = "0123456789abcdef"[n&0xF]
+        else:
+            buf[i] = c
+
+        if bs_char is not None:
+            buf[i] = '\\'
+            i += 1
+            buf[i] = bs_char
+
+    i += 1
+    buf[i] = quote
+
+    return space.wrap("".join(buf[:i+1])) # buffer was overallocated, so slice
+
    
 app = gateway.applevel(r'''
     def str_translate__String_ANY_ANY(s, table, deletechars=''):
@@ -942,25 +985,6 @@
         L =  [ table[ord(s[i])] for i in range(len(s)) if s[i] not in deletechars ]
         return ''.join(L)
 
-    def repr__String(s):
-        quote = "'"
-        if quote in s and '"' not in s:
-            quote = '"'
-        repr = quote
-        for c in s:
-            if c == '\\' or c == quote: 
-                repr += '\\'+c
-            elif c == '\t': repr += '\\t'
-            elif c == '\r': repr += '\\r'
-            elif c == '\n': repr += '\\n'
-            elif not '\x20' <= c < '\x7f':
-                n = ord(c)
-                repr += '\\x'+"0123456789abcdef"[n>>4]+"0123456789abcdef"[n&0xF]
-            else:
-                repr += c
-        repr += quote
-        return repr
-
     def str_decode__String_ANY_ANY(str, encoding=None, errors=None):
         import codecs
         if encoding is None and errors is None:
@@ -1001,7 +1025,6 @@
 str_translate__String_ANY_ANY = app.interphook('str_translate__String_ANY_ANY') 
 str_decode__String_ANY_ANY = app.interphook('str_decode__String_ANY_ANY') 
 str_encode__String_ANY_ANY = app.interphook('str_encode__String_ANY_ANY') 
-repr__String = app.interphook('repr__String') 
 mod__String_ANY = app2.interphook('mod__String_ANY') 
 
 # register all methods



More information about the Pypy-commit mailing list