Commit d0e8ab0a authored by Stefan Behnel's avatar Stefan Behnel

enable iter-dict optimisation also for a plain 'for x in dict', assign dict...

enable iter-dict optimisation also for a plain 'for x in dict', assign dict ref to temp var before entering the loop to avoid re-assignment problems
parent f6743b62
...@@ -42,6 +42,12 @@ class DictIterTransform(Visitor.VisitorTransform): ...@@ -42,6 +42,12 @@ class DictIterTransform(Visitor.VisitorTransform):
def visit_ForInStatNode(self, node): def visit_ForInStatNode(self, node):
self.visitchildren(node) self.visitchildren(node)
iterator = node.iterator.sequence iterator = node.iterator.sequence
if iterator.type is Builtin.dict_type:
# like iterating over dict.keys()
dict_obj = iterator
keys = True
values = False
else:
if not isinstance(iterator, ExprNodes.SimpleCallNode): if not isinstance(iterator, ExprNodes.SimpleCallNode):
return node return node
function = iterator.function function = iterator.function
...@@ -65,6 +71,9 @@ class DictIterTransform(Visitor.VisitorTransform): ...@@ -65,6 +71,9 @@ class DictIterTransform(Visitor.VisitorTransform):
py_object_ptr = PyrexTypes.c_void_ptr_type py_object_ptr = PyrexTypes.c_void_ptr_type
temps = [] temps = []
temp = UtilNodes.TempHandle(PyrexTypes.py_object_type)
temps.append(temp)
dict_temp = temp.ref(dict_obj.pos)
pos_temp = node.iterator.counter pos_temp = node.iterator.counter
pos_temp_addr = ExprNodes.AmpersandNode( pos_temp_addr = ExprNodes.AmpersandNode(
node.pos, operand=pos_temp, node.pos, operand=pos_temp,
...@@ -157,6 +166,10 @@ class DictIterTransform(Visitor.VisitorTransform): ...@@ -157,6 +166,10 @@ class DictIterTransform(Visitor.VisitorTransform):
pos = node.pos, pos = node.pos,
lhs = pos_temp, lhs = pos_temp,
rhs = ExprNodes.IntNode(node.pos, value=0)), rhs = ExprNodes.IntNode(node.pos, value=0)),
Nodes.SingleAssignmentNode(
pos = dict_obj.pos,
lhs = dict_temp,
rhs = dict_obj),
Nodes.WhileStatNode( Nodes.WhileStatNode(
pos = node.pos, pos = node.pos,
condition = ExprNodes.SimpleCallNode( condition = ExprNodes.SimpleCallNode(
...@@ -167,7 +180,7 @@ class DictIterTransform(Visitor.VisitorTransform): ...@@ -167,7 +180,7 @@ class DictIterTransform(Visitor.VisitorTransform):
name = self.PyDict_Next_name, name = self.PyDict_Next_name,
type = self.PyDict_Next_func_type, type = self.PyDict_Next_func_type,
entry = self.PyDict_Next_entry), entry = self.PyDict_Next_entry),
args = [dict_obj, pos_temp_addr, args = [dict_temp, pos_temp_addr,
key_temp_addr, value_temp_addr] key_temp_addr, value_temp_addr]
), ),
body = body, body = body,
......
...@@ -10,6 +10,8 @@ __doc__ = u""" ...@@ -10,6 +10,8 @@ __doc__ = u"""
[(10, 0), (11, 1), (12, 2), (13, 3)] [(10, 0), (11, 1), (12, 2), (13, 3)]
>>> iterkeys(d) >>> iterkeys(d)
[10, 11, 12, 13] [10, 11, 12, 13]
>>> iterdict(d)
[10, 11, 12, 13]
>>> itervalues(d) >>> itervalues(d)
[0, 1, 2, 3] [0, 1, 2, 3]
""" """
...@@ -42,6 +44,13 @@ def iterkeys(dict d): ...@@ -42,6 +44,13 @@ def iterkeys(dict d):
l.sort() l.sort()
return l return l
def iterdict(dict d):
l = []
for k in d:
l.append(k)
l.sort()
return l
def itervalues(dict d): def itervalues(dict d):
l = [] l = []
for v in d.itervalues(): for v in d.itervalues():
......
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