Commit 1a4c6b5d authored by Stefan Behnel's avatar Stefan Behnel

optimise min/max([...]) with sequence literal argument

parent b5a2401a
...@@ -1554,8 +1554,11 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform): ...@@ -1554,8 +1554,11 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
"""Replace min(a,b,...) and max(a,b,...) by explicit comparison code. """Replace min(a,b,...) and max(a,b,...) by explicit comparison code.
""" """
if len(args) <= 1: if len(args) <= 1:
# leave this to Python if len(args) == 1 and args[0].is_sequence_constructor:
return node args = args[0].args
else:
# leave this to Python
return node
cascaded_nodes = list(map(UtilNodes.ResultRefNode, args[1:])) cascaded_nodes = list(map(UtilNodes.ResultRefNode, args[1:]))
......
...@@ -20,6 +20,40 @@ def min3(a,b,c): ...@@ -20,6 +20,40 @@ def min3(a,b,c):
""" """
return min(a,b,c) return min(a,b,c)
@cython.test_assert_path_exists("//CondExprNode")
@cython.test_fail_if_path_exists("//SimpleCallNode")
def min3_list(a,b,c):
"""
>>> min3_list(1,2,3)
1
>>> min3_list(2,3,1)
1
>>> min3_list(2,1,3)
1
>>> min3_list(3,1,2)
1
>>> min3_list(3,2,1)
1
"""
return min([a,b,c])
@cython.test_assert_path_exists("//CondExprNode")
@cython.test_fail_if_path_exists("//SimpleCallNode")
def min3_tuple(a,b,c):
"""
>>> min3_tuple(1,2,3)
1
>>> min3_tuple(2,3,1)
1
>>> min3_tuple(2,1,3)
1
>>> min3_tuple(3,1,2)
1
>>> min3_tuple(3,2,1)
1
"""
return min((a,b,c))
@cython.test_assert_path_exists("//CondExprNode") @cython.test_assert_path_exists("//CondExprNode")
@cython.test_fail_if_path_exists("//SimpleCallNode") @cython.test_fail_if_path_exists("//SimpleCallNode")
def min3_typed(int a, int b, int c): def min3_typed(int a, int b, int c):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment