diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2021-06-03 16:28:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-03 16:28:27 (GMT) |
commit | 2c1e2583fdc4db6b43d163239ea42b0e8394171f (patch) | |
tree | b4becea668a3bccc7ffbfcee3ca6b712d14f2131 /Python/suggestions.c | |
parent | ea0210fa8ccca769896847f25fc6fadfe9a717bc (diff) | |
download | cpython-2c1e2583fdc4db6b43d163239ea42b0e8394171f.zip cpython-2c1e2583fdc4db6b43d163239ea42b0e8394171f.tar.gz cpython-2c1e2583fdc4db6b43d163239ea42b0e8394171f.tar.bz2 |
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
Diffstat (limited to 'Python/suggestions.c')
-rw-r--r-- | Python/suggestions.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Python/suggestions.c b/Python/suggestions.c index 2e76551..43c0ef0 100644 --- a/Python/suggestions.c +++ b/Python/suggestions.c @@ -3,6 +3,7 @@ #include "pycore_frame.h" #include "pycore_pyerrors.h" +#include "pycore_code.h" // _PyCode_GetVarnames() #define MAX_CANDIDATE_ITEMS 750 #define MAX_STRING_SIZE 40 @@ -210,8 +211,12 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc) PyFrameObject *frame = traceback->tb_frame; assert(frame != NULL); PyCodeObject *code = PyFrame_GetCode(frame); - assert(code != NULL && code->co_varnames != NULL); - PyObject *dir = PySequence_List(code->co_varnames); + assert(code != NULL && code->co_localsplusnames != NULL); + PyObject *varnames = _PyCode_GetVarnames(code); + if (varnames == NULL) { + return NULL; + } + PyObject *dir = PySequence_List(varnames); Py_DECREF(code); if (dir == NULL) { return NULL; |