Commit 72cb404f authored by Stefan Behnel's avatar Stefan Behnel

Optimise list.extend([...]) into list.extend((...)) since tuples are more efficient than lists.

parent 67b3ef81
......@@ -2829,12 +2829,16 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
if len(args) != 2:
return node
obj, value = args
if not value.is_sequence_constructor or value.mult_factor is not None:
if not value.is_sequence_constructor:
return node
items = list(value.args)
if len(items) > 4:
# Appending wins for short sequences.
if value.mult_factor is not None or len(items) > 4:
# Appending wins for short sequences but might slow down for multiple resize operations.
# Ignorantly assume that this a good enough limit that avoids repeated resizing.
if isinstance(value, ExprNodes.ListNode):
# At least avoid the list building and use a faster tuple instead.
tuple_node = args[1].as_tuple().analyse_types(self.current_env(), skip_children=True)
Visitor.recursively_replace_node(node, args[1], tuple_node)
return node
wrapped_obj = self._wrap_self_arg(obj, function, is_unbound_method, 'extend')
if not items:
......
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