Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy Markdown
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