GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||
GeezerVison is showing github.com — bpo-33234 Improve list() pre-sizing for inputs with known lengths by pablogsal · Pull Request #6493 · python/cpython Conversation
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This should be marked as a CPython only test.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@rhettinger Amended in 7602879
Sorry, something went wrong.
There was an error while loading. Please reload this page.
The list() constructor isn't taking full advantage of known input lengths or length hints. This commit makes the constructor pre-size and not over-allocate when the input size is known or can be reasonably estimated. A new test is added to test_list.py and a test needed to be modified in test_descr.py as it assumed that there is only one call to __length_hint__() in the list constructor.
| (void)_list_clear(self); | ||
| } | ||
| if (iterable != NULL) { | ||
| Py_ssize_t iter_len = PyObject_LengthHint(iterable, 0); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
list_extend() uses PyObject_LengthHint(iterable, 8): we may also use 8 here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I have collected more metrics. This are the L1 and L2 cache misses and related information: THIS PR MASTER As you can see, this shows a great improvement in cache efficiency and branch predictions. As there is some concern regarding what happens with the overhead of calling And these are the results: As you can see the only case when this patch is slow is for very small iterables without |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
CC: @rhettinger |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I'm not sure about this change because of the possible slowdown for short datasets. Raymond and me proposed to add a slot for length hint. I would suggest to work in two steps:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Agreed. Will open a new PR with the version that only uses lenght if it is available. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.