Commit e45283ff authored by Stefan Behnel's avatar Stefan Behnel

optimise 'for in [...]' into 'for in (...)'

parent 6184fab9
......@@ -3122,6 +3122,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
node.if_clauses = if_clauses
return node
def visit_ForInStatNode(self, node):
self.visitchildren(node)
# iterating over a list literal? => tuples are more efficient
sequence = node.iterator.sequence
if isinstance(sequence, ExprNodes.ListNode):
node.iterator.sequence = ExprNodes.TupleNode(
sequence.pos, args=sequence.args, mult_factor=sequence.mult_factor)
return node
# in the future, other nodes can have their own handler method here
# that can replace them with a constant result node
......
......@@ -2,6 +2,7 @@
# tag: forin
import sys
import cython
try:
from builtins import next
......@@ -36,6 +37,18 @@ def for_in_list():
[1, 2, 3, 4, 5]
"""
@cython.test_assert_path_exists('//TupleNode//IntNode')
@cython.test_fail_if_path_exists('//ListNode//IntNode')
def for_in_literal_list():
"""
>>> for_in_literal_list()
[1, 2, 3, 4]
"""
l = []
for i in [1,2,3,4]:
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