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
9c9c12c0
Commit
9c9c12c0
authored
Apr 28, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise away conditional expressions (x if cond else y) based on constant condition results
parent
39aa2269
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
0 deletions
+11
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+9
-0
tests/run/ifelseexpr_T267.pyx
tests/run/ifelseexpr_T267.pyx
+2
-0
No files found.
Cython/Compiler/Optimize.py
View file @
9c9c12c0
...
@@ -3138,6 +3138,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
...
@@ -3138,6 +3138,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
return
ExprNodes
.
BoolNode
(
node
.
pos
,
value
=
bool_result
,
return
ExprNodes
.
BoolNode
(
node
.
pos
,
value
=
bool_result
,
constant_result
=
bool_result
)
constant_result
=
bool_result
)
def
visit_CondExprNode
(
self
,
node
):
self
.
_calculate_const
(
node
)
if
node
.
test
.
constant_result
is
ExprNodes
.
not_a_constant
:
return
node
if
node
.
test
.
constant_result
:
return
node
.
true_val
else
:
return
node
.
false_val
def
visit_IfStatNode
(
self
,
node
):
def
visit_IfStatNode
(
self
,
node
):
self
.
visitchildren
(
node
)
self
.
visitchildren
(
node
)
# eliminate dead code based on constant condition results
# eliminate dead code based on constant condition results
...
...
tests/run/ifelseexpr_T267.pyx
View file @
9c9c12c0
...
@@ -36,6 +36,7 @@ def nested(x):
...
@@ -36,6 +36,7 @@ def nested(x):
"""
"""
return
1
if
x
==
1
else
(
2
if
x
==
2
else
3
)
return
1
if
x
==
1
else
(
2
if
x
==
2
else
3
)
@
cython
.
test_fail_if_path_exists
(
'//CondExprNode'
)
def
const_true
():
def
const_true
():
"""
"""
>>> const_true()
>>> const_true()
...
@@ -43,6 +44,7 @@ def const_true():
...
@@ -43,6 +44,7 @@ def const_true():
"""
"""
return
1
if
1
==
1
else
2
return
1
if
1
==
1
else
2
@
cython
.
test_fail_if_path_exists
(
'//CondExprNode'
)
def
const_false
():
def
const_false
():
"""
"""
>>> const_false()
>>> const_false()
...
...
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