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
f629adda
Commit
f629adda
authored
Jul 16, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite of min()/max() optimisation, now correctly handling temps and types
parent
de519dc7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
170 additions
and
36 deletions
+170
-36
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+35
-36
tests/run/min_max_optimization.pyx
tests/run/min_max_optimization.pyx
+135
-0
No files found.
Cython/Compiler/Optimize.py
View file @
f629adda
...
@@ -1803,50 +1803,49 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
...
@@ -1803,50 +1803,49 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
if
len
(
args
)
<=
1
:
if
len
(
args
)
<=
1
:
# leave this to Python
# leave this to Python
return
node
return
node
unpacked_args
=
[]
unpacked_args
=
[]
for
arg
in
args
:
for
arg
in
args
:
if
isinstance
(
arg
,
ExprNodes
.
CoerceToPyTypeNode
):
if
isinstance
(
arg
,
ExprNodes
.
CoerceToPyTypeNode
):
arg
=
arg
.
arg
arg
=
arg
.
arg
unpacked_args
.
append
(
arg
)
unpacked_args
.
append
(
arg
)
spanning_type
=
reduce
(
PyrexTypes
.
spanning_type
,
[
arg
.
type
for
arg
in
unpacked_args
])
is_pycompare
=
spanning_type
.
is_pyobject
result_ref
=
UtilNodes
.
ResultRefNode
(
pos
=
node
.
pos
,
type
=
spanning_type
)
stats
=
[
Nodes
.
SingleAssignmentNode
(
node
.
pos
,
lhs
=
UtilNodes
.
ResultRefNode
(
pos
=
node
.
pos
,
expression
=
result_ref
),
rhs
=
unpacked_args
[
0
].
coerce_to
(
spanning_type
,
self
.
current_env
()),
first
=
True
)
]
for
arg
in
unpacked_args
[
1
:]:
arg_nodes
=
[]
stats
.
append
(
Nodes
.
IfStatNode
(
ref_nodes
=
[]
arg
.
pos
,
spanning_type
=
PyrexTypes
.
spanning_type
(
unpacked_args
[
0
].
type
,
unpacked_args
[
1
].
type
)
else_clause
=
None
,
for
arg
in
unpacked_args
:
if_clauses
=
[
Nodes
.
IfClauseNode
(
arg
=
arg
.
coerce_to
(
spanning_type
,
self
.
current_env
())
if
not
isinstance
(
arg
,
ExprNodes
.
ConstNode
):
arg
=
UtilNodes
.
LetRefNode
(
arg
)
ref_nodes
.
append
(
arg
)
arg_nodes
.
append
(
arg
)
spanning_type
=
PyrexTypes
.
spanning_type
(
spanning_type
,
arg
.
type
)
last_result
=
arg_nodes
[
0
]
for
arg_node
in
arg_nodes
[
1
:]:
last_result
=
last_result
.
coerce_to
(
arg_node
.
type
,
self
.
current_env
())
is_py_compare
=
arg_node
.
type
.
is_pyobject
last_result
=
ExprNodes
.
CondExprNode
(
arg_node
.
pos
,
type
=
arg_node
.
type
,
# already coerced, so this is the target type
is_temp
=
True
,
true_val
=
arg_node
,
false_val
=
last_result
,
test
=
ExprNodes
.
PrimaryCmpNode
(
arg
.
pos
,
arg
.
pos
,
condition
=
ExprNodes
.
PrimaryCmpNode
(
operand1
=
arg_node
,
arg
.
pos
,
operator
=
operator
,
operand1
=
arg
.
coerce_to
(
spanning_type
,
self
.
current_env
()),
operand2
=
last_result
,
operator
=
operator
,
is_pycmp
=
is_py_compare
,
operand2
=
result_ref
,
is_temp
=
is_py_compare
,
is_pycmp
=
is_pycompare
,
type
=
is_py_compare
and
PyrexTypes
.
py_object_type
or
PyrexTypes
.
c_bint_type
,
is_temp
=
is_pycompare
,
).
coerce_to_boolean
(
self
.
current_env
()).
coerce_to_temp
(
self
.
current_env
()),
type
=
is_pycompare
and
PyrexTypes
.
py_object_type
or
PyrexTypes
.
c_bint_type
)
).
coerce_to_boolean
(
self
.
current_env
()),
body
=
Nodes
.
SingleAssignmentNode
(
for
ref_node
in
ref_nodes
[::
-
1
]:
arg
.
pos
,
last_result
=
UtilNodes
.
EvalWithTempExprNode
(
ref_node
,
last_result
)
lhs
=
result_ref
,
rhs
=
arg
)
)]
))
return
UtilNodes
.
TempResultFromStatNode
(
return
last_result
.
coerce_to
(
node
.
type
,
self
.
current_env
())
result_ref
,
Nodes
.
StatListNode
(
node
.
pos
,
stats
=
stats
)
).
coerce_to
(
node
.
type
,
self
.
current_env
())
### special methods
### special methods
...
...
tests/run/min_max_optimization.pyx
0 → 100644
View file @
f629adda
class
loud_list
(
list
):
def
__len__
(
self
):
print
"calling __len__"
return
super
(
loud_list
,
self
).
__len__
()
# max()
def
test_max2
():
"""
>>> test_max2()
2
2
2
2
2
calling __len__
3
calling __len__
3
"""
cdef
int
my_int
=
1
cdef
object
my_pyint
=
2
cdef
object
my_list
=
loud_list
([
1
,
2
,
3
])
print
max
(
1
,
2
)
print
max
(
2
,
my_int
)
print
max
(
my_int
,
2
)
print
max
(
my_int
,
my_pyint
)
print
max
(
my_pyint
,
my_int
)
print
max
(
my_int
,
len
(
my_list
))
print
max
(
len
(
my_list
),
my_int
)
def
test_max3
():
"""
>>> test_max3()
calling __len__
3
calling __len__
calling __len__
3
"""
cdef
int
my_int
=
1
cdef
object
my_pyint
=
2
cdef
object
my_list
=
loud_list
([
1
,
2
,
3
])
print
max
(
my_int
,
my_pyint
,
len
(
my_list
))
print
max
(
my_pyint
,
my_list
.
__len__
(),
len
(
my_list
))
def
test_maxN
():
"""
>>> test_maxN()
calling __len__
3
calling __len__
3
calling __len__
3
"""
cdef
int
my_int
=
1
cdef
object
my_pyint
=
2
cdef
object
my_list
=
loud_list
([
1
,
2
,
3
])
print
max
(
my_int
,
2
,
my_int
,
0
,
my_pyint
,
my_int
,
len
(
my_list
))
print
max
(
my_int
,
my_int
,
0
,
my_pyint
,
my_int
,
len
(
my_list
))
print
max
(
my_int
,
my_int
,
2
,
my_int
,
0
,
my_pyint
,
my_int
,
len
(
my_list
))
# min()
def
test_min2
():
"""
>>> test_min2()
1
1
1
1
1
calling __len__
1
calling __len__
1
"""
cdef
int
my_int
=
1
cdef
object
my_pyint
=
2
cdef
object
my_list
=
loud_list
([
1
,
2
,
3
])
print
min
(
1
,
2
)
print
min
(
2
,
my_int
)
print
min
(
my_int
,
2
)
print
min
(
my_int
,
my_pyint
)
print
min
(
my_pyint
,
my_int
)
print
min
(
my_int
,
len
(
my_list
))
print
min
(
len
(
my_list
),
my_int
)
def
test_min3
():
"""
>>> test_min3()
calling __len__
1
calling __len__
calling __len__
2
"""
cdef
int
my_int
=
1
cdef
object
my_pyint
=
2
cdef
object
my_list
=
loud_list
([
1
,
2
,
3
])
print
min
(
my_int
,
my_pyint
,
len
(
my_list
))
print
min
(
my_pyint
,
my_list
.
__len__
(),
len
(
my_list
))
def
test_minN
():
"""
>>> test_minN()
calling __len__
0
calling __len__
0
calling __len__
0
"""
cdef
int
my_int
=
1
cdef
object
my_pyint
=
2
cdef
object
my_list
=
loud_list
([
1
,
2
,
3
])
print
min
(
my_int
,
2
,
my_int
,
0
,
my_pyint
,
my_int
,
len
(
my_list
))
print
min
(
my_int
,
my_int
,
0
,
my_pyint
,
my_int
,
len
(
my_list
))
print
min
(
my_int
,
my_int
,
2
,
my_int
,
0
,
my_pyint
,
my_int
,
len
(
my_list
))
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