From a6a2d569b7526ebc3f21d16fe777fa4cbbf75621 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 3 Jul 2009 20:31:40 +0200 Subject: [PATCH] faster call to builtin type() --- Cython/Compiler/Optimize.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 315d8572..8c20674e 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -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 -- 2.26.2