osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Write this accumuator in a functional style


On 7/14/17, Steve D'Aprano <steve+python at pearwood.info> wrote:
> On Fri, 14 Jul 2017 09:06 am, Ned Batchelder wrote:
>
>> Steve's summary is qualitatively right, but a little off on the
>> quantitative
>> details.  Lists don't resize to 2*N, they resize to ~1.125*N:
>>
>>     new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 :
>> 6);
>>
>> (https://github.com/python/cpython/blob/master/Objects/listobject.c#L49-L58)
>
> Ah, thanks for the correction. I was going off vague memories of long-ago
> discussion (perhaps even as long ago as Python 1.5!) when Tim Peters (I
> think
> it was) described how list overallocation worked.

You could remember it from sets:

    return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);

(https://github.com/python/cpython/blob/master/Objects/setobject.c#L239)