]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92914: Round the allocated size for lists up to the even number (GH-92915) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 7 Jun 2022 08:54:40 +0000 (01:54 -0700)
committerGitHub <noreply@github.com>
Tue, 7 Jun 2022 08:54:40 +0000 (10:54 +0200)
(cherry picked from commit 8a6af5a34642f5564220eb50d72caada8f17fc78)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_sys.py
Misc/NEWS.d/next/Core and Builtins/2022-05-18-08-32-33.gh-issue-92914.tJUeTD.rst [new file with mode: 0644]
Objects/listobject.c

index a0458092553b4c9d78c40fc21dc1476e2929b6dc..3c362485d9fe06db284fc04faf174ca8f2d55db1 100644 (file)
@@ -1317,9 +1317,10 @@ class SizeofTest(unittest.TestCase):
         import re
         check(re.finditer('',''), size('2P'))
         # list
-        samples = [[], [1,2,3], ['1', '2', '3']]
-        for sample in samples:
-            check(list(sample), vsize('Pn') + len(sample)*self.P)
+        check(list([]), vsize('Pn'))
+        check(list([1]), vsize('Pn') + 2*self.P)
+        check(list([1, 2]), vsize('Pn') + 2*self.P)
+        check(list([1, 2, 3]), vsize('Pn') + 4*self.P)
         # sortwrapper (list)
         # XXX
         # cmpwrapper (list)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-18-08-32-33.gh-issue-92914.tJUeTD.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-18-08-32-33.gh-issue-92914.tJUeTD.rst
new file mode 100644 (file)
index 0000000..1242a15
--- /dev/null
@@ -0,0 +1 @@
+Always round the allocated size for lists up to the nearest even number.
index 7f37b7386051d0ebe25cb0145c5665fb3bfdcb2d..329fd1f9741a58f8b88ec8f9fc623258819ad85b 100644 (file)
@@ -95,6 +95,12 @@ 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 on 64-bit
+     * platforms (8 on 32-bit), there is no benefit of allocating space for
+     * the odd number of items, and there is no drawback of rounding the
+     * allocated size up to the nearest even number.
+     */
+    size = (size + 1) & ~(size_t)1;
     PyObject **items = PyMem_New(PyObject*, size);
     if (items == NULL) {
         PyErr_NoMemory();