8000 gh-92914: Round the allocated size for lists up to the even number by serhiy-storchaka · Pull Request #92915 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-92914: Round the allocated size for lists up to the even number #92915

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

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000 Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

There are no files selected for viewing

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Always round up the size allocated for lists up to 16 bytes.
10 changes: 10 additions & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
assert(self->ob_item == NULL);
assert(size > 0);

/* Since the Python memory allocator has granularity of 16 bytes,
Copy link
Member

Choose a reason for hiding this comment

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

alignment is 8bytes on 32bit platform.

cpython/Objects/obmalloc.c

Lines 878 to 884 in b86d783

#if SIZEOF_VOID_P > 4
#define ALIGNMENT 16 /* must be 2^N */
#define ALIGNMENT_SHIFT 4
#else
#define ALIGNMENT 8 /* must be 2^N */
#define ALIGNMENT_SHIFT 3
#endif

So I think we don't need the else { block.

* there is no benefit of allocating space for the odd number of items,
* and there is no drawback of rounding it up.
*/
if (sizeof(PyObject*) > 4) {
size = (size + 1) & ~(size_t)1;
}
else {
size = (size + 3) & ~(size_t)3;
}
PyObject **items = PyMem_New(PyObject*, size);
if (items == NULL) {
PyErr_NoMemory();
Expand Down
0