optimise dict(some_dict) into a call to PyDict_Copy()
authorStefan Behnel <scoder@users.berlios.de>
Sun, 29 Mar 2009 16:44:04 +0000 (18:44 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Sun, 29 Mar 2009 16:44:04 +0000 (18:44 +0200)
Cython/Compiler/Optimize.py

index 3fcc03ad2376e0026714dbc7f49f8b2490eb3097..f009f49f239bd509b9b1698bb1f74daaf13d4174 100644 (file)
@@ -515,6 +515,25 @@ class OptimiseBuiltinCalls(Visitor.VisitorTransform):
             return node
         return kwargs
 
+    PyDict_Copy_func_type = PyrexTypes.CFuncType(
+        Builtin.dict_type, [
+            PyrexTypes.CFuncTypeArg("dict", Builtin.dict_type, None)
+            ])
+
+    def _handle_simple_function_dict(self, node, pos_args):
+        """Replace dict(some_dict) by PyDict_Copy(some_dict).
+        """
+        if len(pos_args.args) != 1:
+            return node
+        if pos_args.args[0].type is not Builtin.dict_type:
+            return node
+
+        return ExprNodes.PythonCapiCallNode(
+            node.pos, "PyDict_Copy", self.PyDict_Copy_func_type,
+            args = pos_args.args,
+            is_temp = node.is_temp
+            )
+
     def _handle_simple_function_set(self, node, pos_args):
         """Replace set([a,b,...]) by a literal set {a,b,...}.
         """