Commit 5b6497c8 authored by Stefan Behnel's avatar Stefan Behnel

Increase number of items for which list.extend([...]) is optimised....

Increase number of items for which list.extend([...]) is optimised. Benchmarking shows that append() is still faster for 8 items (on an empty list) than extend().
This limit could be increased for larger lists (which grow by a larger amount), but if we have to settle on one value then 8 is good enough, as we can expect literal item lists to be rather short.
parent 07249180
...@@ -2832,9 +2832,9 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2832,9 +2832,9 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
if not value.is_sequence_constructor: if not value.is_sequence_constructor:
return node return node
items = list(value.args) items = list(value.args)
if value.mult_factor is not None or len(items) > 4: if value.mult_factor is not None or len(items) > 8:
# Appending wins for short sequences but might slow down for multiple resize operations. # Appending wins for short sequences but slows down when multiple resize operations are needed.
# Ignorantly assume that this a good enough limit that avoids repeated resizing. # This seems to be a good enough limit that avoids repeated resizing.
if isinstance(value, ExprNodes.ListNode): if isinstance(value, ExprNodes.ListNode):
# At least avoid the list building and use a faster tuple instead. # 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) tuple_node = args[1].as_tuple().analyse_types(self.current_env(), skip_children=True)
......
...@@ -180,13 +180,13 @@ def test_list_pop_all(): ...@@ -180,13 +180,13 @@ def test_list_pop_all():
def test_list_extend(seq=None, x=4): def test_list_extend(seq=None, x=4):
""" """
>>> test_list_extend() >>> test_list_extend()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> test_list_extend([]) >>> test_list_extend([])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> test_list_extend([1]) >>> test_list_extend([1])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]
>>> test_list_extend([1, 2]) >>> test_list_extend([1, 2])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2]
""" """
cdef list l = [1,2,3] cdef list l = [1,2,3]
l.extend([]) l.extend([])
...@@ -195,7 +195,7 @@ def test_list_extend(seq=None, x=4): ...@@ -195,7 +195,7 @@ def test_list_extend(seq=None, x=4):
assert l == [1,2,3] assert l == [1,2,3]
assert len(l) == 3 assert len(l) == 3
l.extend([4,x+1,6]) l.extend([4,x+1,6])
l.extend([7,8,9,10,11,12,13,14]) l.extend([7,8,9,10,11,12,13,14,15,16])
if seq is not None: if seq is not None:
l.extend(seq) l.extend(seq)
return l return l
...@@ -208,13 +208,13 @@ def test_list_extend(seq=None, x=4): ...@@ -208,13 +208,13 @@ def test_list_extend(seq=None, x=4):
def test_list_extend_unbound(seq=None, x=4): def test_list_extend_unbound(seq=None, x=4):
""" """
>>> test_list_extend_unbound() >>> test_list_extend_unbound()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> test_list_extend_unbound([]) >>> test_list_extend_unbound([])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> test_list_extend_unbound([1]) >>> test_list_extend_unbound([1])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]
>>> test_list_extend_unbound([1, 2]) >>> test_list_extend_unbound([1, 2])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2]
""" """
cdef list l = [1,2,3] cdef list l = [1,2,3]
list.extend(l, []) list.extend(l, [])
...@@ -229,7 +229,7 @@ def test_list_extend_unbound(seq=None, x=4): ...@@ -229,7 +229,7 @@ def test_list_extend_unbound(seq=None, x=4):
assert l == [1,2,3] assert l == [1,2,3]
assert len(l) == 3 assert len(l) == 3
list.extend(l, [4,x+1,6]) list.extend(l, [4,x+1,6])
list.extend(l, [7,8,9,10,11,12,13,14]) list.extend(l, [7,8,9,10,11,12,13,14,15,16])
if seq is not None: if seq is not None:
list.extend(l, seq) list.extend(l, seq)
return l return l
......
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