python / cpython

The Python programming language
https://www.python.org
Other
62.75k stars 30.07k forks source link

split mutable and immutable parts of code objects #102802

Open carljm opened 1 year ago

carljm commented 1 year ago

Code objects are mostly immutable, but have some mutable parts: co_code_adaptive, co_extra, _co_cached, and co_filename (I think this is all on first glance, may have missed something.)

It would be useful to split the immutable and mutable parts of the code object, so as to be able to maximize sharing; see discussion in https://github.com/faster-cpython/ideas/issues/466

The mutable part should have a pointer to the immutable part, rather than the other way around; this leaves open the possibility of sharing the immutable part between sub-interpreters.

carljm commented 1 year ago

Doing https://github.com/python/cpython/issues/101346 might also help to simplify this a bit.

iritkatriel commented 1 year ago

Why is filename mutable?

carljm commented 1 year ago

Why is filename mutable?

Because a precompiled pyc file may be imported from a different file system location than where it was originally compiled, so on import we fix up co_filename to match the imported-from location; see the call to _imp._fix_co_filename in importlib.

This falls into the category of "idempotent constant" since it is only set once at import time and then should remain immutable thereafter. (EDIT: or maybe not? it is technically mutable at Python level, not just C level.) In any case, from the perspective of deep freezing it has to be considered mutable.