Question Details

No question body available.

Tags

python python-internals

Answers (1)

Accepted Answer Available
Accepted Answer
May 23, 2026 Score: 5 Rep: 42,545 Quality: Expert Completeness: 60%

Why LOADFAST is not used

In answer to question 2, root-level exec-ed code is not able to use LOADFAST and STOREFAST instructions. This is because it is executed as if it were a module object. This means it always uses the LOADNAME and STORENAME instructions. LOADFAST and STOREFAST cannot be used as adaptive instructions for *NAME instructions as they work on different data structures. _FAST works on arrays and NAME works on mappings. If you wanted to do this specialisation you need to change the backing data store from map to an array. You would also need to alter any functions that use the namespace of the module for its globals. Otherwise they would try to access the array as if it were map. At best cause a crash or at worst carry on with a silent bug. However, since the module might not have a handle to these functions any more, this is impossible. So the code must stick to using the *NAME instructions.

The speedup you are looking for is also trivially available by wrapping your code in a function. When you do this the interpreter will automatically uses the _FAST instructions, without requiring complex logic to adapt the NAME instructions. For example:

def test
dynamicfast(): locals = {} exec(""" def f(): #