Commit 93440ae2 authored by Stefan Behnel's avatar Stefan Behnel

Avoid calling PySequence_Tuple() if what we have is exactly a tuple.

parent 51a20816
...@@ -6264,6 +6264,7 @@ class AsTupleNode(ExprNode): ...@@ -6264,6 +6264,7 @@ class AsTupleNode(ExprNode):
# arg ExprNode # arg ExprNode
subexprs = ['arg'] subexprs = ['arg']
is_temp = 1
def calculate_constant_result(self): def calculate_constant_result(self):
self.constant_result = tuple(self.arg.constant_result) self.constant_result = tuple(self.arg.constant_result)
...@@ -6280,7 +6281,6 @@ class AsTupleNode(ExprNode): ...@@ -6280,7 +6281,6 @@ class AsTupleNode(ExprNode):
if self.arg.type is tuple_type: if self.arg.type is tuple_type:
return self.arg.as_none_safe_node("'NoneType' object is not iterable") return self.arg.as_none_safe_node("'NoneType' object is not iterable")
self.type = tuple_type self.type = tuple_type
self.is_temp = 1
return self return self
def may_be_none(self): def may_be_none(self):
...@@ -6290,10 +6290,11 @@ class AsTupleNode(ExprNode): ...@@ -6290,10 +6290,11 @@ class AsTupleNode(ExprNode):
gil_message = "Constructing Python tuple" gil_message = "Constructing Python tuple"
def generate_result_code(self, code): def generate_result_code(self, code):
cfunc = "__Pyx_PySequence_Tuple" if self.arg.type in (py_object_type, tuple_type) else "PySequence_Tuple"
code.putln( code.putln(
"%s = PySequence_Tuple(%s); %s" % ( "%s = %s(%s); %s" % (
self.result(), self.result(),
self.arg.py_result(), cfunc, self.arg.py_result(),
code.error_goto_if_null(self.result(), self.pos))) code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result()) code.put_gotref(self.py_result())
......
...@@ -2210,14 +2210,10 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2210,14 +2210,10 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
PyrexTypes.CFuncTypeArg("list", Builtin.list_type, None) PyrexTypes.CFuncTypeArg("list", Builtin.list_type, None)
]) ])
PySequence_Tuple_func_type = PyrexTypes.CFuncType(
Builtin.tuple_type,
[PyrexTypes.CFuncTypeArg("it", PyrexTypes.py_object_type, None)])
def _handle_simple_function_tuple(self, node, function, pos_args): def _handle_simple_function_tuple(self, node, function, pos_args):
"""Replace tuple([...]) by PyList_AsTuple or PySequence_Tuple. """Replace tuple([...]) by PyList_AsTuple or PySequence_Tuple.
""" """
if len(pos_args) != 1: if len(pos_args) != 1 or not node.is_temp:
return node return node
arg = pos_args[0] arg = pos_args[0]
if arg.type is Builtin.tuple_type and not arg.may_be_none(): if arg.type is Builtin.tuple_type and not arg.may_be_none():
...@@ -2230,9 +2226,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2230,9 +2226,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
node.pos, "PyList_AsTuple", self.PyList_AsTuple_func_type, node.pos, "PyList_AsTuple", self.PyList_AsTuple_func_type,
args=pos_args, is_temp=node.is_temp) args=pos_args, is_temp=node.is_temp)
else: else:
return ExprNodes.PythonCapiCallNode( return ExprNodes.AsTupleNode(node.pos, arg=arg, type=Builtin.tuple_type)
node.pos, "PySequence_Tuple", self.PySequence_Tuple_func_type,
args=pos_args, is_temp=node.is_temp)
PySet_New_func_type = PyrexTypes.CFuncType( PySet_New_func_type = PyrexTypes.CFuncType(
Builtin.set_type, [ Builtin.set_type, [
......
...@@ -92,6 +92,9 @@ static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u) ...@@ -92,6 +92,9 @@ static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x); static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x);
#define __Pyx_PySequence_Tuple(obj) \
(likely(PyTuple_CheckExact(obj)) ? __Pyx_NewRef(obj) : PySequence_Tuple(obj))
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
......
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