10000 gh-92914: Always round up the size allocated for lists up to 16 bytes · serhiy-storchaka/cpython@9a2ed09 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9a2ed09

Browse files
pythongh-92914: Always round up the size allocated for lists up to 16 bytes
1 parent b86d783 commit 9a2ed09

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Always round up the size allocated for lists up to 16 bytes.

Objects/listobject.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ list_preallocate_exact(PyListObject *self, Py_ssize_t size)
9494
assert(self->ob_item == NULL);
9595
assert(size > 0);
9696

97+
/* Since the Python memory allocator has granularity of 16 bytes,
98+
* there is no benefit of allocating space for the odd number of items,
99+
* and there is no drawback of rounding it up.
100+
*/
101+
if (sizeof(PyObject*) > 4) {
102+
size = (size + 1) & ~(size_t)1;
103+
}
104+
else {
105+
size = (size + 3) & ~(size_t)3;
106+
}
97107
PyObject **items = PyMem_New(PyObject*, size);
98108
if (items == NULL) {
99109
PyErr_NoMemory();

0 commit comments

Comments
 (0)
0