[pypy-commit] pypy default: only use a single counter in xrange iterators (should save a setitem)

hakanardo noreply at buildbot.pypy.org
Thu Nov 3 13:11:51 CET 2011


Author: Hakan Ardo <hakan at debian.org>
Branch: 
Changeset: r48694:a27a481ec877
Date: 2011-11-03 13:11 +0100
http://bitbucket.org/pypy/pypy/changeset/a27a481ec877/

Log:	only use a single counter in xrange iterators (should save a
	setitem)

diff --git a/pypy/module/__builtin__/functional.py b/pypy/module/__builtin__/functional.py
--- a/pypy/module/__builtin__/functional.py
+++ b/pypy/module/__builtin__/functional.py
@@ -312,10 +312,11 @@
 
 
 class W_XRange(Wrappable):
-    def __init__(self, space, start, len, step):
+    def __init__(self, space, start, stop, step):
         self.space = space
         self.start = start
-        self.len   = len
+        self.stop = stop
+        self.len = get_len_of_range(space, start, stop, step)
         self.step  = step
 
     def descr_new(space, w_subtype, w_start, w_stop=None, w_step=1):
@@ -325,9 +326,8 @@
             start, stop = 0, start
         else:
             stop = _toint(space, w_stop)
-        howmany = get_len_of_range(space, start, stop, step)
         obj = space.allocate_instance(W_XRange, w_subtype)
-        W_XRange.__init__(obj, space, start, howmany, step)
+        W_XRange.__init__(obj, space, start, stop, step)
         return space.wrap(obj)
 
     def descr_repr(self):
@@ -357,12 +357,12 @@
 
     def descr_iter(self):
         return self.space.wrap(W_XRangeIterator(self.space, self.start,
-                                                self.len, self.step))
+                                                self.stop, self.step))
 
     def descr_reversed(self):
         lastitem = self.start + (self.len-1) * self.step
         return self.space.wrap(W_XRangeIterator(self.space, lastitem,
-                                                self.len, -self.step))
+                                                self.start - 1, -self.step))
 
     def descr_reduce(self):
         space = self.space
@@ -389,25 +389,24 @@
 )
 
 class W_XRangeIterator(Wrappable):
-    def __init__(self, space, current, remaining, step):
+    def __init__(self, space, start, stop, step):
         self.space = space
-        self.current = current
-        self.remaining = remaining
+        self.current = start
+        self.stop = stop
         self.step = step
 
     def descr_iter(self):
         return self.space.wrap(self)
 
     def descr_next(self):
-        if self.remaining > 0:
+        if (self.step > 0 and self.current < self.stop) or (self.step < 0 and self.current > self.stop):
             item = self.current
             self.current = item + self.step
-            self.remaining -= 1
             return self.space.wrap(item)
         raise OperationError(self.space.w_StopIteration, self.space.w_None)
 
-    def descr_len(self):
-        return self.space.wrap(self.remaining)
+    #def descr_len(self):
+    #    return self.space.wrap(self.remaining)
 
     def descr_reduce(self):
         from pypy.interpreter.mixedmodule import MixedModule
@@ -418,7 +417,7 @@
         w        = space.wrap
         nt = space.newtuple
 
-        tup = [w(self.current), w(self.remaining), w(self.step)]
+        tup = [w(self.current), w(self.stop), w(self.step)]
         return nt([new_inst, nt(tup)])
 
 W_XRangeIterator.typedef = TypeDef("rangeiterator",
diff --git a/pypy/module/_pickle_support/maker.py b/pypy/module/_pickle_support/maker.py
--- a/pypy/module/_pickle_support/maker.py
+++ b/pypy/module/_pickle_support/maker.py
@@ -66,10 +66,10 @@
     new_generator.running = running
     return space.wrap(new_generator)
 
- at unwrap_spec(current=int, remaining=int, step=int)
-def xrangeiter_new(space, current, remaining, step):
+ at unwrap_spec(current=int, stop=int, step=int)
+def xrangeiter_new(space, current, stop, step):
     from pypy.module.__builtin__.functional import W_XRangeIterator
-    new_iter = W_XRangeIterator(space, current, remaining, step)
+    new_iter = W_XRangeIterator(space, current, stop, step)
     return space.wrap(new_iter)
 
 @unwrap_spec(identifier=str)


More information about the pypy-commit mailing list