[pypy-svn] r74031 - pypy/branch/rpython-iterator/pypy/objspace/std

fijal at codespeak.net fijal at codespeak.net
Fri Apr 23 19:54:55 CEST 2010


Author: fijal
Date: Fri Apr 23 19:54:53 2010
New Revision: 74031

Modified:
   pypy/branch/rpython-iterator/pypy/objspace/std/objspace.py
   pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py
Log:
A quick hack to measure performance - use newly created iterator for unicode
join


Modified: pypy/branch/rpython-iterator/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/branch/rpython-iterator/pypy/objspace/std/objspace.py	(original)
+++ pypy/branch/rpython-iterator/pypy/objspace/std/objspace.py	Fri Apr 23 19:54:53 2010
@@ -36,7 +36,21 @@
 from pypy.objspace.std.inttype import wrapint
 from pypy.objspace.std.stringtype import wrapstr
 from pypy.objspace.std.unicodetype import wrapunicode
+from pypy.rlib.r_iter import r_iter, list_iter
 
+class iterate_w_root(r_iter):
+    def __init__(self, space, w_iterable):
+        self.w_iterator = space.iter(w_iterable)
+        self.space = space
+
+    def next(self):
+        space = self.space
+        try:
+            return space.next(self.w_iterator)
+        except OperationError, oe:
+            if oe.match(space, space.w_StopIteration):
+                raise StopIteration
+            raise
 
 class StdObjSpace(ObjSpace, DescrOperation):
     """The standard object space, implementing a general-purpose object
@@ -347,6 +361,14 @@
             raise self._wrap_expected_length(expected_length, len(t))
         return t
 
+    def iterate_w(self, w_iterable):
+        """ Returns a thing that can be iterated over. No more
+        operations are allowed, primarily, this is not a list
+        """
+        if isinstance(w_iterable, W_ListObject):
+            return list_iter(w_iterable.wrappeditems)
+        return iterate_w_root(self, w_iterable)
+
     def fixedview(self, w_obj, expected_length=-1):
         """ Fast paths
         """

Modified: pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/branch/rpython-iterator/pypy/objspace/std/unicodeobject.py	Fri Apr 23 19:54:53 2010
@@ -181,27 +181,23 @@
     return space.newbool(container.find(item) != -1)
 
 def unicode_join__Unicode_ANY(space, w_self, w_list):
-    l = space.unpackiterable(w_list)
-    delim = w_self._value
-    totlen = 0
-    if len(l) == 0:
-        return W_UnicodeObject.EMPTY
-    if (len(l) == 1 and
-        space.is_w(space.type(l[0]), space.w_unicode)):
-        return l[0]
+    #if len(l) == 0:
+    #    return W_UnicodeObject.EMPTY
+    #if (len(l) == 1 and
+    #    space.is_w(space.type(l[0]), space.w_unicode)):
+    #    return l[0]
     
-    values_list = [None] * len(l)
-    for i in range(len(l)):
-        item = l[i]
-        if isinstance(item, W_UnicodeObject):
+    values_list = []#None] * len(l)
+    for w_item in space.iterate_w(w_list):
+        if isinstance(w_item, W_UnicodeObject):
             # shortcut for performane
-            item = item._value
-        elif space.is_true(space.isinstance(item, space.w_str)):
-            item = space.unicode_w(item)
+            item = w_item._value
+        elif space.is_true(space.isinstance(w_item, space.w_str)):
+            item = space.unicode_w(w_item)
         else:
             raise operationerrfmt(space.w_TypeError,
                 "sequence item %d: expected string or Unicode", i)
-        values_list[i] = item
+        values_list.append(item)
     return W_UnicodeObject(w_self._value.join(values_list))
 
 def hash__Unicode(space, w_uni):



More information about the Pypy-commit mailing list