cpython/Objects/clinic
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
..
bytearrayobject.c.h bpo-42431: Fix outdated bytes comments (GH-23458) 2020-12-03 12:46:16 +02:00
bytesobject.c.h bpo-42431: Fix outdated bytes comments (GH-23458) 2020-12-03 12:46:16 +02:00
codeobject.c.h bpo-43693: Add new internal code objects fields: co_fastlocalnames and co_fastlocalkinds. (gh-26388) 2021-06-03 10:28:27 -06:00
complexobject.c.h bpo-42161: Use _PyLong_GetZero() and _PyLong_GetOne() (GH-22995) 2020-10-27 02:24:34 +01:00
descrobject.c.h bpo-36127: Argument Clinic: inline parsing code for keyword parameters. (GH-12058) 2019-03-14 10:32:22 +02:00
dictobject.c.h bpo-41333: Convert OrderedDict.pop() to Argument Clinic (GH-21534) 2020-07-19 09:18:55 +03:00
enumobject.c.h bpo-36127: Argument Clinic: inline parsing code for keyword parameters. (GH-12058) 2019-03-14 10:32:22 +02:00
floatobject.c.h bpo-42161: Use _PyLong_GetZero() and _PyLong_GetOne() (GH-22995) 2020-10-27 02:24:34 +01:00
funcobject.c.h bpo-37034: Display argument name on errors with keyword arguments with Argument Clinic. (GH-13593) 2019-08-29 17:49:08 +03:00
listobject.c.h bpo-40792: Make the result of PyNumber_Index() always having exact type int. (GH-20443) 2020-05-28 10:33:45 +03:00
longobject.c.h Revert "bpo-26680: Incorporate is_integer in all built-in and standard library numeric types (GH-6121)" (GH-22584) 2020-10-07 16:43:44 -07:00
memoryobject.c.h bpo-41262: Convert memoryview to Argument Clinic. (GH-21421) 2020-07-18 11:12:05 +03:00
moduleobject.c.h bpo-37034: Display argument name on errors with keyword arguments with Argument Clinic. (GH-13593) 2019-08-29 17:49:08 +03:00
odictobject.c.h bpo-41333: Convert OrderedDict.pop() to Argument Clinic (GH-21534) 2020-07-19 09:18:55 +03:00
structseq.c.h bpo-36127: Argument Clinic: inline parsing code for keyword parameters. (GH-12058) 2019-03-14 10:32:22 +02:00
tupleobject.c.h bpo-35582: Argument Clinic: Optimize the "all boring objects" case. (GH-11520) 2019-01-11 18:01:42 +02:00
typeobject.c.h bpo-37999: No longer use __int__ in implicit integer conversions. (GH-15636) 2020-05-26 18:43:38 +03:00
unicodeobject.c.h bpo-41334: Convert constructors of str, bytes and bytearray to Argument Clinic (GH-21535) 2020-07-20 15:53:55 +03:00