cpython/Lib/importlib
Eric Snow 2c1e2583fd
bpo-43693: Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388)
A number of places in the code base (notably ceval.c and frameobject.c) rely on mapping variable names to indices in the frame "locals plus" array (AKA fast locals), and thus opargs.  Currently the compiler indirectly encodes that information on the code object as the tuples co_varnames, co_cellvars, and co_freevars.  At runtime the dependent code must calculate the proper mapping from those, which isn't ideal and impacts performance-sensitive sections.  This is something we can easily address in the compiler instead.

This change addresses the situation by replacing internal use of co_varnames, etc. with a single combined tuple of names in locals-plus order, along with a minimal array mapping each to its kind (local vs. cell vs. free).  These two new PyCodeObject fields, co_fastlocalnames and co_fastllocalkinds, are not exposed to Python code for now, but co_varnames, etc. are still available with the same values as before (though computed lazily).

Aside from the (mild) performance impact, there are a number of other benefits:

* there's now a clear, direct relationship between locals-plus and variables
* code that relies on the locals-plus-to-name mapping is simpler
* marshaled code objects are smaller and serialize/de-serialize faster

Also note that we can take this approach further by expanding the possible values in co_fastlocalkinds to include specific argument types (e.g. positional-only, kwargs).  Doing so would allow further speed-ups in _PyEval_MakeFrameVector(), which is where args get unpacked into the locals-plus array.  It would also allow us to shrink marshaled code objects even further.

https://bugs.python.org/issue43693
2021-06-03 10:28:27 -06:00
..
metadata bpo-44246: Restore compatibility in entry_points (GH-26468) 2021-05-31 12:19:42 -04:00
__init__.py bpo-42135: Deprecate implementations of find_module() and find_loader() (GH-25169) 2021-04-06 08:56:57 -07:00
_abc.py bpo-42136: Deprecate module_repr() as found in importlib (GH-25022) 2021-03-26 11:55:07 -07:00
_adapters.py bpo-42129: Add support for resources in namespaces (GH-24670) 2021-03-04 13:43:00 -05:00
_bootstrap.py bpo-42135: Deprecate implementations of find_module() and find_loader() (GH-25169) 2021-04-06 08:56:57 -07:00
_bootstrap_external.py bpo-43693: Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388) 2021-06-03 10:28:27 -06:00
_common.py bpo-38693: Prefer f-strings in importlib.resources (importlib_resources 5.0.6). (GH-26387) 2021-05-26 13:16:11 -07:00
abc.py bpo-42135: Deprecate implementations of find_module() and find_loader() (GH-25169) 2021-04-06 08:56:57 -07:00
machinery.py bpo-42403: Fix pyflakes warnings in importlib (GH-23396) 2020-11-19 11:20:57 +01:00
readers.py bpo-38693: Prefer f-strings in importlib.resources (importlib_resources 5.0.6). (GH-26387) 2021-05-26 13:16:11 -07:00
resources.py bpo-38693: Prefer f-strings in importlib.resources (importlib_resources 5.0.6). (GH-26387) 2021-05-26 13:16:11 -07:00
util.py bpo-43720: Update import-related stdlib deprecation messages to say they will be removed in Python 3.12 (GH-25167) 2021-04-03 15:31:15 -07:00