Commit 9ecf58c8 authored by Stefan Behnel's avatar Stefan Behnel

pre-calculate constant loop length when iterating over literal tuples/lists

parent cc8a670b
......@@ -2337,11 +2337,14 @@ class IteratorNode(ExprNode):
def generate_next_sequence_item(self, test_name, result_name, code):
assert self.counter_cname, "internal error: counter_cname temp not prepared"
code.putln(
"if (%s >= Py%s_GET_SIZE(%s)) break;" % (
self.counter_cname,
test_name,
self.py_result()))
final_size = 'Py%s_GET_SIZE(%s)' % (test_name, self.py_result())
if self.sequence.is_sequence_constructor:
item_count = len(self.sequence.args)
if self.sequence.mult_factor is None:
final_size = item_count
elif isinstance(self.sequence.mult_factor.constant_result, (int, long)):
final_size = item_count * self.sequence.mult_factor.constant_result
code.putln("if (%s >= %s) break;" % (self.counter_cname, final_size))
if self.reversed:
inc_dec = '--'
else:
......
......@@ -49,6 +49,18 @@ def for_in_literal_list():
l.append(i)
return l
@cython.test_assert_path_exists('//TupleNode//IntNode')
@cython.test_fail_if_path_exists('//ListNode//IntNode')
def for_in_literal_mult_list():
"""
>>> for_in_literal_mult_list()
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
"""
l = []
for i in [1,2,3,4] * 3:
l.append(i)
return l
class Iterable(object):
"""
>>> for_in_pyiter(Iterable(5))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment