[Python-ideas] pop multiple elements of a list at once
ncoghlan at gmail.com
Mon Jul 12 14:45:10 CEST 2010
On Mon, Jul 12, 2010 at 4:35 PM, Guido van Rossum <guido at python.org> wrote:
> I think yo misunderstand the implementation of lists (and the
> underlying malloc()). You can't break the memory used for the list
> elements into two pieces and give new ownership to a (leading) section
> of it. However you also seem to be worrying about "copying" too much
> -- the only things that get copied are the *pointers* to the objects
> popped off the stack, which is very cheap compared to the rest of the
> operation. It is true that to pop off a whole slice there is a more
> efficient way than calling pop() repeatedly -- but there's no need for
> a new primitive operation, as it can already be done by copying and
> then deleting the slice (again, the copying only copies the pointers).
Note that the original poster was apparently talking about
array.array() rather than an actual list (at least, that's the way I
interpreted the phrase "array,Array() list"). In that context, the
desire to avoid copying when invoking pop() makes a lot more sense
than it does when using a builtin list.
I agree that the suggestion of reassigning ownership of a chunk of an
array is based on a misunderstanding of the way memory allocation
works at the pymalloc and OS levels though.
For the record, neither pymalloc nor the OS support breaking a chunk
of already allocated memory in two that way - you need some master
object to maintain control of it, and then use other pointers to look
at subsections. Since memoryview objects in 3.x and 2.7 are designed
specifically to provide a window onto a chunk of memory owned by
another object (such as the storage underlying an array object)
without copying, it seems like that is the kind of thing the original
poster is actually looking for.
(That said, supporting slice objects in pop() still doesn't strike me
as an insane idea, although I'd probably want to see some use cases
before we went to the hassle of adding it).
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas