Sunday, October 30, 2005

Dynamic Linking for Module Attributes

In much Python code, foo.bar refers to some attribute of the imported module "foo". There is only ever one instance of any module, so could attributes of modules be given pointers somewhere in the module object (internally) and thus importing scripts can be compiled such that foo.bar would be resolved to some new bytecode that says "load the object pointed to at this location". The location would be null when compiled, but importing would consult a table mapping the module's attribute references in the script to the locations in the bytecode of those null pointers. This would effectively work like dynamic linking in C/C++, and would reduce a lot of attribute lookups to a pair of pointer dereferences.

No comments: