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?
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 machine s traditionally operate, which I believe CPython is (at least partially) modelled after, eg What does it mean that python is stack based?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.