Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
082156ee
Commit
082156ee
authored
May 16, 2009
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enumerate fixes: single-statement bodies, avoid redundant deep recursion during loop optimisation
parent
131e416d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
11 deletions
+36
-11
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+21
-11
tests/run/enumerate_T316.pyx
tests/run/enumerate_T316.pyx
+15
-0
No files found.
Cython/Compiler/Optimize.py
View file @
082156ee
...
...
@@ -72,6 +72,9 @@ class IterationTransform(Visitor.VisitorTransform):
def
visit_ForInStatNode
(
self
,
node
):
self
.
visitchildren
(
node
)
return
self
.
_optimise_for_loop
(
node
)
def
_optimise_for_loop
(
self
,
node
):
iterator
=
node
.
iterator
.
sequence
if
iterator
.
type
is
Builtin
.
dict_type
:
# like iterating over dict.keys()
...
...
@@ -158,23 +161,30 @@ class IterationTransform(Visitor.VisitorTransform):
is_temp
=
counter_type
.
is_pyobject
)
enumerate_assignment_in_loop
=
Nodes
.
SingleAssignmentNode
(
pos
=
enumerate_target
.
pos
,
lhs
=
enumerate_target
,
rhs
=
temp
.
ref
(
enumerate_target
.
pos
))
loop_body
=
[
Nodes
.
SingleAssignmentNode
(
pos
=
enumerate_target
.
pos
,
lhs
=
enumerate_target
,
rhs
=
temp
.
ref
(
enumerate_target
.
pos
)),
Nodes
.
SingleAssignmentNode
(
pos
=
enumerate_target
.
pos
,
lhs
=
temp
.
ref
(
enumerate_target
.
pos
),
rhs
=
inc_expression
)
]
inc_statement
=
Nodes
.
SingleAssignmentNode
(
pos
=
enumerate_target
.
pos
,
lhs
=
temp
.
ref
(
enumerate_target
.
pos
),
rhs
=
inc_expression
)
if
isinstance
(
node
.
body
,
Nodes
.
StatListNode
):
node
.
body
.
stats
=
loop_body
+
node
.
body
.
stats
else
:
loop_body
.
append
(
node
.
body
)
node
.
body
=
Nodes
.
StatListNode
(
node
.
body
.
pos
,
stats
=
loop_body
)
node
.
body
.
stats
.
insert
(
0
,
enumerate_assignment_in_loop
)
node
.
body
.
stats
.
insert
(
1
,
inc_statement
)
node
.
target
=
iterable_target
node
.
iterator
.
sequence
=
enumerate_function
.
arg_tuple
.
args
[
0
]
# recurse into loop to check for further optimisations
node
=
self
.
visit_ForInStatNode
(
node
)
node
=
self
.
_optimise_for_loop
(
node
)
statements
=
[
Nodes
.
SingleAssignmentNode
(
...
...
tests/run/enumerate_T316.pyx
View file @
082156ee
...
...
@@ -53,6 +53,14 @@ __doc__ = u"""
3 4
:: 3 4
>>> py_enumerate_dict({})
:: 55 99
>>> py_enumerate_dict(dict(a=1, b=2, c=3))
0 a
1 c
2 b
:: 2 b
"""
def
go_py_enumerate
():
...
...
@@ -69,6 +77,13 @@ def go_c_enumerate_step():
for
i
,
k
in
enumerate
(
range
(
1
,
7
,
2
)):
print
i
,
k
def
py_enumerate_dict
(
dict
d
):
cdef
int
i
=
55
k
=
99
for
i
,
k
in
enumerate
(
d
):
print
i
,
k
print
u"::"
,
i
,
k
def
py_enumerate_break
(
*
t
):
i
,
k
=
55
,
99
for
i
,
k
in
enumerate
(
t
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment