Commit 54adfe2d authored by Stefan Behnel's avatar Stefan Behnel

optimise set(it) into a call to PySet_New(it)

parent e40d032f
...@@ -1993,9 +1993,14 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform): ...@@ -1993,9 +1993,14 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform):
exception_value = "((double)-1)", exception_value = "((double)-1)",
exception_check = True) exception_check = True)
PySet_New_func_type = PyrexTypes.CFuncType(
Builtin.set_type, [
PyrexTypes.CFuncTypeArg("it", PyrexTypes.py_object_type, None)
])
def _handle_simple_function_set(self, node, function, pos_args): def _handle_simple_function_set(self, node, function, pos_args):
if len(pos_args) == 1 and isinstance(pos_args[0], (ExprNodes.ListNode, if len(pos_args) == 1:
ExprNodes.TupleNode)): if pos_args[0].is_sequence_constructor:
# We can optimise set([x,y,z]) safely into a set literal, # We can optimise set([x,y,z]) safely into a set literal,
# but only if we create all items before adding them - # but only if we create all items before adding them -
# adding an item may raise an exception if it is not # adding an item may raise an exception if it is not
...@@ -2012,6 +2017,15 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform): ...@@ -2012,6 +2017,15 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform):
for temp in temps[::-1]: for temp in temps[::-1]:
result = UtilNodes.EvalWithTempExprNode(temp, result) result = UtilNodes.EvalWithTempExprNode(temp, result)
return result return result
else:
# PySet_New(it) is better than a generic Python call to set(it)
return ExprNodes.PythonCapiCallNode(
node.pos, "PySet_New",
self.PySet_New_func_type,
args=pos_args,
is_temp=node.is_temp,
utility_code=UtilityCode.load_cached('pyset_compat', 'Builtins.c'),
py_name="set")
return node return node
def _handle_simple_function_float(self, node, function, pos_args): def _handle_simple_function_float(self, node, function, pos_args):
......
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