[pypy-svn] r70964 - pypy/trunk/pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Fri Jan 29 12:58:40 CET 2010


Author: arigo
Date: Fri Jan 29 12:58:39 2010
New Revision: 70964

Modified:
   pypy/trunk/pypy/objspace/std/stringobject.py
   pypy/trunk/pypy/objspace/std/unicodeobject.py
Log:
Partial fix for the performance of ``"abc" * count'',
also for unicode.  Note that this version no longer
checks for overflows beforehand; you should get the
overflows as a MemoryError in string_repeat().
It might fail a few of CPython's unit tests.


Modified: pypy/trunk/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/stringobject.py	Fri Jan 29 12:58:39 2010
@@ -11,7 +11,7 @@
 from pypy.objspace.std.listobject import W_ListObject
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.tupleobject import W_TupleObject
-from pypy.rlib.rstring import StringBuilder
+from pypy.rlib.rstring import StringBuilder, string_repeat
 from pypy.interpreter.buffer import StringBuffer
 
 from pypy.objspace.std.stringtype import sliced, joined, wrapstr, wrapchar, \
@@ -852,20 +852,12 @@
     if mul <= 0:
         return W_StringObject.EMPTY
     input = w_str._value
-    input_len = len(input)
-    try:
-        buflen = ovfcheck(mul * input_len)
-    except OverflowError:
-        raise operationerrfmt(
-            space.w_OverflowError, 
-            "repeated string is too long: %d times %d characters",
-            mul, input_len)
-    # XXX maybe only do this when input has a big length
-    # XXX CPython tricks:
-    #       - if input has length 1, use memset
-    #       - otherwise, use memcpy to repeatedly double the size of the
-    #         string (i.e. compute the "square" of the string)
-    return joined(space, [input] * mul)
+    if len(input) == 1:
+        s = input[0] * mul
+    else:
+        s = string_repeat(input, mul)
+    # xxx support again space.config.objspace.std.withstrjoin?
+    return W_StringObject(s)
 
 def mul__String_ANY(space, w_str, w_times):
     return mul_string_times(space, w_str, w_times)

Modified: pypy/trunk/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/unicodeobject.py	Fri Jan 29 12:58:39 2010
@@ -11,6 +11,7 @@
 from pypy.objspace.std.tupleobject import W_TupleObject
 from pypy.rlib.rarithmetic import intmask, ovfcheck
 from pypy.rlib.objectmodel import compute_hash
+from pypy.rlib.rstring import string_repeat
 from pypy.module.unicodedata import unicodedb_4_1_0 as unicodedb
 from pypy.tool.sourcetools import func_with_new_name
 
@@ -260,15 +261,13 @@
         if e.match(space, space.w_TypeError):
             raise FailedToImplement
         raise
-    uni = w_uni._value
-    length = len(uni)
-    if times <= 0 or length == 0:
-        return W_UnicodeObject.EMPTY
-    try:
-        result_size = ovfcheck(length * times)
-        result = u''.join([uni] * times)
-    except OverflowError:
-        raise OperationError(space.w_OverflowError, space.wrap('repeated string is too long'))
+    if times <= 0:
+        return W_StringObject.EMPTY
+    input = w_uni._value
+    if len(input) == 1:
+        result = input[0] * times
+    else:
+        result = string_repeat(input, times)
     return W_UnicodeObject(result)
 
 def mul__ANY_Unicode(space, w_times, w_uni):



More information about the Pypy-commit mailing list