Skip to content
Merged
Prev Previous commit
Next Next commit
Fix NEWS entry and correct style
  • Loading branch information
pablogsal committed Oct 15, 2018
commit ab450b3d5a453f36a4988d83b391a79bfbfa5a61
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
The list constructor will pre-size and not over-allocate when the input size
is known or can be reasonably estimated.
The list constructor will pre-size and not over-allocate when
the input lenght is known.
2 changes: 1 addition & 1 deletion Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2676,7 +2676,7 @@ list___init___impl(PyListObject *self, PyObject *iterable)
}
if (iterable != NULL) {
Py_ssize_t iter_len = PyObject_Length(iterable);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce or avoid the overhead of the worst case, you can avoid this call if _PyObject_HasLen() is false.

This change doesn't require to implement a new _PyObject_ProbeLength().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated run of first experiment using _PyObject_HasLen():

Mean +- std dev: [old] 795 ns +- 25 ns -> [new] 776 ns +- 25 ns: 1.02x faster (-6%)

if (iter_len == -1){
if (iter_len == -1) {
PyErr_Clear();
}
if (iter_len > 0 && list_preallocate_exact(self, iter_len)) {
Expand Down