How does the Python's stack work when dealing with tuples/lists?

1.6k views Asked by At

I saw in Python documentation that the BUILD_TUPLE instruction "Creates a tuple consuming count items from the stack, and pushes the resulting tuple onto the stack."

It really pushes the tuple itself? What if the tuple contains a large number of elements? How it is placed on stack?

2

There are 2 answers

0
juanpa.arrivillaga On

This answer applies to CPython specifically, but all CPython objects live on a private heap.

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.

At the lowest level, a raw memory allocator ensures that there is enough room in the private heap for storing all Python-related data by interacting with the memory manager of the operating system. On top of the raw memory allocator, several object-specific allocators operate on the same heap and implement distinct memory management policies adapted to the peculiarities of every object type. For example, integer objects are managed differently within the heap than strings, tuples or dictionaries because integers imply different storage requirements and speed/space tradeoffs. The Python memory manager thus delegates some of the work to the object-specific allocators, but ensures that the latter operate within the bounds of the private heap.

Note, everything in Python is an object. The only thing going onto the interpreter stack is a PyObject pointer. The stack here being an implementation detail of the CPython runtime. Source code is compiled to bytecode, which is executed on this stack-based virtual machine.

0
Sam Mason On

further to @juanpa.arrivillaga's answer and me playing with the dis module for the first timeā€¦

it might be instructive to disassemble the trivial function:

def foo(a, b, c):
  return (a, b, c)

which results in:

  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                1 (b)
              4 LOAD_FAST                2 (c)
              6 BUILD_TUPLE              3
              8 RETURN_VALUE

in other words: we're making sure the stack has the correct parameter values on the top, then pops them all off and replaces them with a (reference to a) single tuple.

this is how stack machines traditionally operate, which I believe CPython is (at least partially) modelled after, e.g. What does it mean that python is stack based?