[Python-ideas] Fast sum() for non-numbers

Sergey sergemp at mail.ru
Thu Jul 11 00:47:38 CEST 2013


On Jul 9, 2013 Ronald Oussoren wrote:

>> For example, rewrite tuple to internally store its values in a
>> list, and have `localcount` variable saying how many items from
>> that list belong to this tuple. Then __add__ could extend that
>> list and reuse it for new tuple.
> 
> That's not going to happen, not only breaks that backward compatibility
> for users for the C API,

It depends on implementation details, it's possible to keep it
backward compatible. BTW, what C API do you expect to break?

> it has nasty side effects and is incorrect.
> Nasty side effect:
> a = (1,)
> b = (2,) * 1000
> c = a + b
> del b
> del c
> With the internal list 'a' keeps alive the extra storage used for 'c'.

Yes, technically it's possible to implement tuple so that it would
realloc internal list to save some ram, but why? List does not do
that when you remove elements from it, why should tuple do that?

On the other hand:
  a = (1,) * 1000
  b = a + (2,3)
  c = b + (4,5)
And you have 3 variables for the price of one. Lot's of memory saved!

> Incorrect:
> a = (1,)
> b = a + (2,)
> c = a + (3,)
> Now 'b' and 'c' can't possibly both share storage with 'a'.

Nothing incorrect here, of course __add__ should handle that, and
if it cannot reuse list it would copy it. As it does now.

Such tuple would never allocate more RAM than before, often it
should use either same RAM or less. In some rare cases it may
not free some RAM, that it could free before. But I'm not sure
they worth the effort fixing.

PS: I don't think anybody wants to see that implementation anyway,
I guess it was just an attempt to bug me with "you can't".

—— 


More information about the Python-ideas mailing list