Skip to content

Add padding in list allocation #92914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
serhiy-storchaka opened this issue May 18, 2022 · 0 comments · Fixed by #92915
Closed

Add padding in list allocation #92914

serhiy-storchaka opened this issue May 18, 2022 · 0 comments · Fixed by #92915
Labels
3.10 only security fixes 3.11 only security fixes 3.12 only security fixes performance Performance or resource usage type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

Since the Python memory allocator has granularity of 16 bytes, allocating 24 bytes does not save memory in comparison with allocating 32 bytes. One of changes in #82554 was to round the allocated size for list up to 4 items (16 bytes on 32-bit platform and 32 bytes of 64-bit platform). #87740 reverted this. Therefore, if you create an empty list, then extend it with 1 item two times, you will need two allocations, even if the first allocation has a space for yet one item (but list does not know about this).

>>> import sys
>>> a = []
>>> sys.getsizeof(a)
56
>>> a += [1]
>>> sys.getsizeof(a)
64
>>> a += [2]
>>> sys.getsizeof(a)
120
@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error performance Performance or resource usage 3.11 only security fixes 3.10 only security fixes 3.12 only security fixes labels May 18, 2022
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue May 18, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 19, 2022
…ber (pythonGH-92915)

(cherry picked from commit 8a6af5a)

Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 19, 2022
…ber (pythonGH-92915)

(cherry picked from commit 8a6af5a)

Co-authored-by: Serhiy Storchaka <[email protected]>
ambv pushed a commit that referenced this issue Jun 7, 2022
miss-islington added a commit that referenced this issue Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 only security fixes performance Performance or resource usage type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant