Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r73299:3ffe46ffd7ba
Date: 2014-09-04 15:29 +0200
http://bitbucket.org/pypy/pypy/changeset/3ffe46ffd7ba/

Log:    The list comprehension optimization was not enabled in RPython loops
        over strings (only lists and dicts).

diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -304,10 +304,10 @@
     def hint(self, *args_s):
         hints = args_s[-1].const
         if 'maxlength' in hints:
-            # only for iteration over lists or dicts at the moment,
+            # only for iteration over lists or dicts or strs at the moment,
             # not over an iterator object (because it has no known length)
             s_iterable = args_s[0]
-            if isinstance(s_iterable, (SomeList, SomeDict)):
+            if isinstance(s_iterable, (SomeList, SomeDict, SomeString)):
                 self = SomeList(self.listdef) # create a fresh copy
                 self.listdef.resize()
                 self.listdef.listitem.hint_maxlength = True
diff --git a/rpython/translator/test/test_simplify.py 
b/rpython/translator/test/test_simplify.py
--- a/rpython/translator/test/test_simplify.py
+++ b/rpython/translator/test/test_simplify.py
@@ -328,6 +328,12 @@
         interp = LLInterpreter(t.rtyper)
         return interp, graph
 
+    def no_resize(self, graph):
+        for block in graph.iterblocks():
+            for op in block.operations:
+                if op.opname == 'direct_call':
+                    assert 'list_resize' not in repr(op.args[0])
+
     def test_simple(self):
         def main(n):
             lst = [x*17 for x in range(n)]
@@ -335,6 +341,16 @@
         interp, graph = self.specialize(main, [int])
         res = interp.eval_graph(graph, [10])
         assert res == 5 * 17
+        self.no_resize(graph)
+
+    def test_str2list(self):
+        def main(n):
+            lst = [c for c in str(n)]
+            return len(lst)
+        interp, graph = self.specialize(main, [int])
+        res = interp.eval_graph(graph, [1091283])
+        assert res == 7
+        self.no_resize(graph)
 
     def test_simple_non_exact(self):
         def main(n):
@@ -343,6 +359,7 @@
         interp, graph = self.specialize(main, [int])
         res = interp.eval_graph(graph, [10])
         assert res == 5
+        self.no_resize(graph)
 
     def test_mutated_after_listcomp(self):
         def main(n):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to