Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r73300:433eafa3297b
Date: 2014-09-04 15:34 +0200
http://bitbucket.org/pypy/pypy/changeset/433eafa3297b/

Log:    Iterating over an RPython string can load the length of the string
        only once

diff --git a/rpython/rtyper/lltypesystem/rstr.py 
b/rpython/rtyper/lltypesystem/rstr.py
--- a/rpython/rtyper/lltypesystem/rstr.py
+++ b/rpython/rtyper/lltypesystem/rstr.py
@@ -1213,6 +1213,7 @@
     external_item_repr = char_repr
     lowleveltype = Ptr(GcStruct('stringiter',
                                 ('string', string_repr.lowleveltype),
+                                ('length', Signed),
                                 ('index', Signed)))
 
 class UnicodeIteratorRepr(BaseStringIteratorRepr):
@@ -1220,6 +1221,7 @@
     external_item_repr = unichar_repr
     lowleveltype = Ptr(GcStruct('unicodeiter',
                                 ('string', unicode_repr.lowleveltype),
+                                ('length', Signed),
                                 ('index', Signed)))
 
 def ll_striter(string):
@@ -1231,16 +1233,16 @@
         raise TypeError("Unknown string type %s" % (typeOf(string),))
     iter = malloc(TP)
     iter.string = string
+    iter.length = len(string.chars)    # load this value only once
     iter.index = 0
     return iter
 
 def ll_strnext(iter):
-    chars = iter.string.chars
     index = iter.index
-    if index >= len(chars):
+    if index >= iter.length:
         raise StopIteration
     iter.index = index + 1
-    return chars[index]
+    return iter.string.chars[index]
 
 def ll_getnextindex(iter):
     return iter.index
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to