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
eaca39b0
Commit
eaca39b0
authored
Dec 01, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
constant fold some more special cases for list/tuple multiplication
parent
371b3568
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
16 deletions
+37
-16
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+12
-15
tests/run/constant_folding.py
tests/run/constant_folding.py
+25
-1
No files found.
Cython/Compiler/Optimize.py
View file @
eaca39b0
...
...
@@ -3265,24 +3265,21 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
return
new_node
def
visit_MulNode
(
self
,
node
):
self
.
_calculate_const
(
node
)
if
isinstance
(
node
.
operand1
,
(
ExprNodes
.
ListNode
,
ExprNodes
.
TupleNode
)):
sequence_node
=
node
.
operand1
factor
=
node
.
operand2
self
.
_calculate_const
(
factor
)
if
factor
.
constant_result
!=
1
and
sequence_node
.
args
:
sequence_node
.
mult_factor
=
factor
self
.
visitchildren
(
sequence_node
)
return
sequence_node
return
self
.
_calculate_constant_seq
(
node
.
operand1
,
node
.
operand2
)
if
isinstance
(
node
.
operand1
,
ExprNodes
.
IntNode
)
and
\
isinstance
(
node
.
operand2
,
(
ExprNodes
.
ListNode
,
ExprNodes
.
TupleNode
)):
sequence_node
=
node
.
operand2
factor
=
node
.
operand1
self
.
_calculate_const
(
factor
)
return
self
.
_calculate_constant_seq
(
node
.
operand2
,
node
.
operand1
)
return
self
.
visit_BinopNode
(
node
)
def
_calculate_constant_seq
(
self
,
sequence_node
,
factor
):
if
factor
.
constant_result
!=
1
and
sequence_node
.
args
:
if
isinstance
(
factor
.
constant_result
,
(
int
,
long
))
and
factor
.
constant_result
<=
0
:
del
sequence_node
.
args
[:]
else
:
sequence_node
.
mult_factor
=
factor
self
.
visitchildren
(
sequence_node
)
return
sequence_node
return
self
.
visit_BinopNode
(
node
)
def
visit_PrimaryCmpNode
(
self
,
node
):
self
.
_calculate_const
(
node
)
...
...
tests/run/constant_folding.py
View file @
eaca39b0
...
...
@@ -281,7 +281,7 @@ def for_in_nested_listcomp():
@
cython
.
test_fail_if_path_exists
(
"//
ListNode//Int
Node"
,
"//
Mul
Node"
,
)
def
mult_empty_list
():
"""
...
...
@@ -289,3 +289,27 @@ def mult_empty_list():
[]
"""
return
5
*
[]
*
100
@
cython
.
test_fail_if_path_exists
(
"//MulNode"
,
"//ListNode//IntNode"
,
)
def
neg_mult_list
():
"""
>>> neg_mult_list()
[]
"""
return
-
5
*
[
1
,
2
]
*
-
100
@
cython
.
test_fail_if_path_exists
(
"//MulNode"
,
"//ListNode//IntNode"
,
)
def
zero_mult_list
():
"""
>>> zero_mult_list()
[]
"""
return
0
*
[
1
,
2
]
*
0
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