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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
e45283ff
Commit
e45283ff
authored
Sep 11, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise 'for in [...]' into 'for in (...)'
parent
6184fab9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
0 deletions
+22
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+9
-0
tests/run/for_in_iter.py
tests/run/for_in_iter.py
+13
-0
No files found.
Cython/Compiler/Optimize.py
View file @
e45283ff
...
...
@@ -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
...
...
tests/run/for_in_iter.py
View file @
e45283ff
...
...
@@ -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))
...
...
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