faster call to builtin type()
authorStefan Behnel <scoder@users.berlios.de>
Fri, 3 Jul 2009 18:31:40 +0000 (20:31 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Fri, 3 Jul 2009 18:31:40 +0000 (20:31 +0200)
Cython/Compiler/Optimize.py

index 315d857256298857bac8431313ee54bd50850089..8c20674e8664823c18f274f83544ac2ed1c4021f 100644 (file)
@@ -690,7 +690,6 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform):
             ])
 
     def _handle_simple_function_getattr(self, node, pos_args):
-        # not really a builtin *type*, but worth optimising anyway
         args = pos_args.args
         if len(args) == 2:
             node = ExprNodes.PythonCapiCallNode(
@@ -710,6 +709,23 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform):
                   "expected 2 or 3, found %d" % len(args))
         return node
 
+    Pyx_Type_func_type = PyrexTypes.CFuncType(
+        Builtin.type_type, [
+            PyrexTypes.CFuncTypeArg("object", PyrexTypes.py_object_type, None)
+            ])
+
+    def _handle_simple_function_type(self, node, pos_args):
+        args = pos_args.args
+        if len(args) != 1:
+            return node
+        node = ExprNodes.PythonCapiCallNode(
+            node.pos, "__Pyx_Type", self.Pyx_Type_func_type,
+                args = args,
+                is_temp = node.is_temp,
+                utility_code = pytype_utility_code,
+                )
+        return node
+
     ### methods of builtin types
 
     PyObject_Append_func_type = PyrexTypes.CFuncType(
@@ -813,6 +829,17 @@ impl = ""
 )
 
 
+pytype_utility_code = UtilityCode(
+proto = """
+static INLINE PyObject* __Pyx_Type(PyObject* o) {
+    PyObject* type = (PyObject*) Py_TYPE(o);
+    Py_INCREF(type);
+    return type;
+}
+"""
+)
+
+
 class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
     """Calculate the result of constant expressions to store it in
     ``expr_node.constant_result``, and replace trivial cases by their