mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
bpo-38373: Change list overallocating strategy. (GH-18952)
* Add padding to make the allocated size multiple of 4. * Do not overallocate if the new size is closer to overalocated size than to the old size.
This commit is contained in:
parent
d469d666b8
commit
2fe815edd6
2 changed files with 10 additions and 6 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
Chaged list overallocation strategy. It no longer overallocates if the new
|
||||||
|
size is closer to overalocated size than to the old size and adds padding.
|
|
@ -54,15 +54,17 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
|
||||||
* enough to give linear-time amortized behavior over a long
|
* enough to give linear-time amortized behavior over a long
|
||||||
* sequence of appends() in the presence of a poorly-performing
|
* sequence of appends() in the presence of a poorly-performing
|
||||||
* system realloc().
|
* system realloc().
|
||||||
* The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
|
* Add padding to make the allocated size multiple of 4.
|
||||||
|
* The growth pattern is: 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ...
|
||||||
* Note: new_allocated won't overflow because the largest possible value
|
* Note: new_allocated won't overflow because the largest possible value
|
||||||
* is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
|
* is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
|
||||||
*/
|
*/
|
||||||
new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
|
new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
|
||||||
if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
|
/* Do not overallocate if the new size is closer to overalocated size
|
||||||
PyErr_NoMemory();
|
* than to the old size.
|
||||||
return -1;
|
*/
|
||||||
}
|
if (newsize - Py_SIZE(self) > (Py_ssize_t)(new_allocated - newsize))
|
||||||
|
new_allocated = ((size_t)newsize + 3) & ~(size_t)3;
|
||||||
|
|
||||||
if (newsize == 0)
|
if (newsize == 0)
|
||||||
new_allocated = 0;
|
new_allocated = 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue