This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author godlygeek
Recipients brett.cannon, eric.snow, godlygeek, kimiguel, pablogsal, rhettinger, terry.reedy
Date 2022-03-08.05:17:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1646716624.49.0.10850417967.issue39829@roundup.psfhosted.org>
In-reply-to
Content
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__`.
History
Date User Action Args
2022-03-08 05:17:04godlygeeksetrecipients: + godlygeek, brett.cannon, rhettinger, terry.reedy, eric.snow, pablogsal, kimiguel
2022-03-08 05:17:04godlygeeksetmessageid: <1646716624.49.0.10850417967.issue39829@roundup.psfhosted.org>
2022-03-08 05:17:04godlygeeklinkissue39829 messages
2022-03-08 05:17:04godlygeekcreate