Commit 6c21bf3b authored by Stefan Behnel's avatar Stefan Behnel

refactored tuple unpacking code once again, removed loop for trivial list/tuple unpacking case

parent 17bdd442
...@@ -4928,7 +4928,21 @@ class SequenceNode(ExprNode): ...@@ -4928,7 +4928,21 @@ class SequenceNode(ExprNode):
or rhs.type in (tuple_type, list_type) or rhs.type in (tuple_type, list_type)
or not rhs.type.is_builtin_type) or not rhs.type.is_builtin_type)
long_enough_for_a_loop = len(self.unpacked_items) > 3 long_enough_for_a_loop = len(self.unpacked_items) > 3
if special_unpack: if special_unpack:
self.generate_special_parallel_unpacking_code(code, rhs)
code.putln("{")
self.generate_generic_parallel_unpacking_code(
code, rhs, self.unpacked_items, use_loop=long_enough_for_a_loop)
code.putln("}")
for value_node in self.coerced_unpacked_items:
value_node.generate_evaluation_code(code)
for i in range(len(self.args)):
self.args[i].generate_assignment_code(
self.coerced_unpacked_items[i], code)
def generate_special_parallel_unpacking_code(self, code, rhs):
tuple_check = 'likely(PyTuple_CheckExact(%s))' % rhs.py_result() tuple_check = 'likely(PyTuple_CheckExact(%s))' % rhs.py_result()
list_check = 'PyList_CheckExact(%s)' % rhs.py_result() list_check = 'PyList_CheckExact(%s)' % rhs.py_result()
sequence_type_test = '1' sequence_type_test = '1'
...@@ -4943,75 +4957,47 @@ class SequenceNode(ExprNode): ...@@ -4943,75 +4957,47 @@ class SequenceNode(ExprNode):
else: else:
sequence_types = ['Tuple', 'List'] sequence_types = ['Tuple', 'List']
sequence_type_test = "(%s) || (%s)" % (tuple_check, list_check) sequence_type_test = "(%s) || (%s)" % (tuple_check, list_check)
code.putln("#if CYTHON_COMPILING_IN_CPYTHON") code.putln("#if CYTHON_COMPILING_IN_CPYTHON")
code.putln("if (%s) {" % sequence_type_test) code.putln("if (%s) {" % sequence_type_test)
code.putln("PyObject* sequence = %s;" % rhs.py_result()) code.putln("PyObject* sequence = %s;" % rhs.py_result())
# CPython list/tuple => check size
code.putln("Py_ssize_t size = Py_SIZE(sequence);")
code.putln("if (unlikely(size != %d)) {" % len(self.args))
code.globalstate.use_utility_code(raise_too_many_values_to_unpack)
code.putln("if (size > %d) __Pyx_RaiseTooManyValuesError(%d);" % (
len(self.args), len(self.args)))
code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
code.putln("else __Pyx_RaiseNeedMoreValuesError(size);")
code.putln(code.error_goto(self.pos))
code.putln("}")
# unpack items from list/tuple in unrolled loop (can't fail)
if len(sequence_types) == 2: if len(sequence_types) == 2:
code.putln("if (likely(Py%s_CheckExact(sequence))) {" % sequence_types[0]) code.putln("if (likely(Py%s_CheckExact(sequence))) {" % sequence_types[0])
self.generate_special_parallel_unpacking_code( for i, item in enumerate(self.unpacked_items):
code, sequence_types[0], code.putln("%s = Py%s_GET_ITEM(sequence, %d); " % (
use_loop=long_enough_for_a_loop and sequence_types[0] != 'Tuple') item.result(), sequence_types[0], i))
if len(sequence_types) == 2: if len(sequence_types) == 2:
code.putln("} else {") code.putln("} else {")
self.generate_special_parallel_unpacking_code( for i, item in enumerate(self.unpacked_items):
code, sequence_types[1], use_loop=long_enough_for_a_loop) code.putln("%s = Py%s_GET_ITEM(sequence, %d); " % (
item.result(), sequence_types[1], i))
code.putln("}") code.putln("}")
for item in self.unpacked_items:
code.put_incref(item.result(), item.ctype())
rhs.generate_disposal_code(code) rhs.generate_disposal_code(code)
code.putln("} else")
if rhs.type is tuple_type: if rhs.type is tuple_type:
code.putln("if (1) {") # if not a tuple: None => save some code by generating the error directly
code.globalstate.use_utility_code(tuple_unpacking_error_code) code.putln("} else if (1) {")
code.putln("__Pyx_UnpackTupleError(%s, %s); %s" % ( code.globalstate.use_utility_code(
rhs.py_result(), len(self.args), code.error_goto(self.pos))) UtilityCode.load_cached("RaiseNoneIterError", "ObjectHandling.c"))
code.putln("__Pyx_RaiseNoneNotIterableError(); %s" % code.error_goto(self.pos))
code.putln("} else") code.putln("} else")
code.putln("#endif") code.putln("#endif")
code.putln("{")
self.generate_generic_parallel_unpacking_code(
code, rhs, self.unpacked_items, use_loop=long_enough_for_a_loop)
code.putln("}")
for value_node in self.coerced_unpacked_items:
value_node.generate_evaluation_code(code)
for i in range(len(self.args)):
self.args[i].generate_assignment_code(
self.coerced_unpacked_items[i], code)
def generate_special_parallel_unpacking_code(self, code, sequence_type, use_loop):
code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
code.globalstate.use_utility_code(raise_too_many_values_to_unpack)
if use_loop:
# must be at the start of a C block!
code.putln("PyObject** temps[%s] = {%s};" % (
len(self.unpacked_items),
','.join(['&%s' % item.result() for item in self.unpacked_items])))
code.putln("if (unlikely(Py%s_GET_SIZE(sequence) != %d)) {" % (
sequence_type, len(self.args)))
code.putln("if (Py%s_GET_SIZE(sequence) > %d) __Pyx_RaiseTooManyValuesError(%d);" % (
sequence_type, len(self.args), len(self.args)))
code.putln("else __Pyx_RaiseNeedMoreValuesError(Py%s_GET_SIZE(sequence));" % sequence_type)
code.putln(code.error_goto(self.pos))
code.putln("}")
if use_loop:
# shorter code in a loop works better for lists in CPython
counter = code.funcstate.allocate_temp(PyrexTypes.c_py_ssize_t_type, manage_ref=False)
code.putln("for (%s=0; %s < %s; %s++) {" % (
counter, counter, len(self.unpacked_items), counter))
code.putln("PyObject* item = Py%s_GET_ITEM(sequence, %s);" % (
sequence_type, counter))
code.putln("*(temps[%s]) = item;" % counter)
code.put_incref("item", PyrexTypes.py_object_type)
code.putln("}")
code.funcstate.release_temp(counter)
else:
# unrolling the loop is very fast for tuples in CPython
for i, item in enumerate(self.unpacked_items):
code.putln("%s = Py%s_GET_ITEM(sequence, %d); " % (item.result(), sequence_type, i))
code.put_incref(item.result(), item.ctype())
def generate_generic_parallel_unpacking_code(self, code, rhs, unpacked_items, use_loop, terminate=True): def generate_generic_parallel_unpacking_code(self, code, rhs, unpacked_items, use_loop, terminate=True):
code.globalstate.use_utility_code(raise_need_more_values_to_unpack) code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
code.globalstate.use_utility_code(UtilityCode.load_cached("IterFinish", "ObjectHandling.c")) code.globalstate.use_utility_code(UtilityCode.load_cached("IterFinish", "ObjectHandling.c"))
......
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