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,
if not value.is_sequence_constructor:
return node
items = list(value.args)
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 value.mult_factor is not None or len(items) > 8:
# Appending wins for short sequences but slows down when multiple resize operations are needed.
# This seems to be 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)
......
......@@ -180,13 +180,13 @@ def test_list_pop_all():
def test_list_extend(seq=None, x=4):
"""
>>> 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, 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])
[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])
[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]
l.extend([])
......@@ -195,7 +195,7 @@ def test_list_extend(seq=None, x=4):
assert l == [1,2,3]
assert len(l) == 3
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:
l.extend(seq)
return l
......@@ -208,13 +208,13 @@ def test_list_extend(seq=None, x=4):
def test_list_extend_unbound(seq=None, x=4):
"""
>>> 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, 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])
[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])
[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]
list.extend(l, [])
......@@ -229,7 +229,7 @@ def test_list_extend_unbound(seq=None, x=4):
assert l == [1,2,3]
assert len(l) == 3
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:
list.extend(l, seq)
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