Message414724
Pardon me for necroing an old issue, but someone pointed out the surprising behavior of `__len__` being called twice by `list(iterable)`, and it caught my curiosity.
https://github.com/python/cpython/commit/372d705d958964289d762953d0a61622755f5386 made it so that `list.__init__(iterable)` calls `iterable.__len__()` before calling `list.extend()`, to preallocate exactly the right amount of space, rather than allowing `list.extend()` to grow the array. That's because `list.extend()` can over-allocate.
What if instead, we made it so that `list.extend(iterable)` doesn't over-allocate when called on an empty list? In the two places where `list_extend` calls `list_resize` to grow the array, we'd check if `self->ob_item == NULL` and if so call `list_preallocate_exact` instead, and we'd remove the call to `list_preallocate_exact` from `list___init___impl`.
It seems like that ought to achieve the same goal as making `__init__` call preallocate exactly, without requiring the extra call to `__len__`. |
|
Date |
User |
Action |
Args |
2022-03-08 05:17:04 | godlygeek | set | recipients:
+ godlygeek, brett.cannon, rhettinger, terry.reedy, eric.snow, pablogsal, kimiguel |
2022-03-08 05:17:04 | godlygeek | set | messageid: <1646716624.49.0.10850417967.issue39829@roundup.psfhosted.org> |
2022-03-08 05:17:04 | godlygeek | link | issue39829 messages |
2022-03-08 05:17:04 | godlygeek | create | |
|