Tuple slices

jfj jfj at freemail.gr
Thu Jan 27 21:01:29 EST 2005


Nick Coghlan wrote:

> 
> 1. Applies only if you are making large slices, or a lot of slices with 
> each containing at least 3 elements.
>   A view can also *cost* memory, when it looks at a small piece of a 
> large item. The view will keep the entire item alive, even though it 
> needs only a small piece.

That is correct.

> 
> 2. Hell no. The *elements* aren't copied, pointers to the elements are. 
> If you *don't* copy the pointers, then every item access through the 
> view involves an indirection as the index into the original sequence 
> gets calculated.

If you have

x=(1,2,...100001)
y=x[:-1]

then you copy 100000 pointers AND you INCREF them AND you DECREF them
when y dies.

The unfortunate case by (1) would be:

x=(1,2,...100001)
x=x[:1]

> 
> So views *may* save memory in some applications, but are unlikely to 
> save time in any application (except any saving resulting from the 
> memory saving).
> 

They do. If tp_dealloc of a tuple view doesn't decref the pointers.

We should look for what is the most common case.


Gerald

-PS: the PEP for the removal ought to have a ":)" at the end.




More information about the Python-list mailing list