4 ms·
Here it's worth knowing a bit about how Python manages data. Each CPython VM stack frame has three tuples associated with it: * co_consts is the constant/lite
by ubernostrum 8y ago
Here it's worth knowing a bit about how Python manages data.
Each CPython VM stack frame has three tuples associated with it:
* co_consts is the constant/literal names local to the frame
* co_varnames is all other names local to the frame
* co_names is the all nonlocal names referenced in the frame
Under the hood, Python creates a structure called 'fastlocals' which is basically an array of (pointer to) PyObject, where the PyObject at each index of the array is the value corresponding to the name at the same index of co_varnames. LOAD_FAST and STORE_FAST are extremely simple opcodes; they do a tiny bit of error checking, refcount management (increment on load, decrement on store), and then just manipulate 'fastlocals'.
So, for example, 'LOAD_FAST i' consists in its entirety of:
1. Grab the PyObject at fastlocals[i]
2. If it's null, throw an UnboundLocalError
3. Otherwise, increment its refcount and push it to the top of the frame's evaluation stack.