Twelve And A Half Percent
CPython grows a list inside list_resize. The relevant lines are:
/* Bypass realloc() when a previous overallocation is large enough
to accommodate the newsize. ... */
if (allocated >= newsize && newsize >= (allocated >> 1)) {
Py_SET_SIZE(self, newsize);
return 0;
}
/* The growth pattern is: 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, ... */
new_allocated = ((size_t)newsize + (newsize >> 3) + 6) & ~(size_t)3;
Start from a = [] and call a.append(x) twenty times. On which of those twenty appends does the buffer actually get reallocated?
Sign in to answer questions and track your progress
Sign In