summaryrefslogtreecommitdiffstats
path: root/Include/cpython
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-06-03 16:28:27 (GMT)
committerGitHub <noreply@github.com>2021-06-03 16:28:27 (GMT)
commit2c1e2583fdc4db6b43d163239ea42b0e8394171f (patch)
treeb4becea668a3bccc7ffbfcee3ca6b712d14f2131 /Include/cpython
parentea0210fa8ccca769896847f25fc6fadfe9a717bc (diff)
downloadcpython-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 'Include/cpython')
-rw-r--r--Include/cpython/code.h20
1 files changed, 15 insertions, 5 deletions
diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index 5c0fae4..add34f3 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -3,6 +3,8 @@
#endif
typedef uint16_t _Py_CODEUNIT;
+// Each oparg must fit in the second half of _Py_CODEUNIT, hence 8 bits.
+#define _Py_MAX_OPARG 255
#ifdef WORDS_BIGENDIAN
# define _Py_OPCODE(word) ((word) >> 8)
@@ -14,6 +16,11 @@ typedef uint16_t _Py_CODEUNIT;
typedef struct _PyOpcache _PyOpcache;
+
+// These are duplicated from pycore_code.h.
+typedef unsigned char _PyLocalsPlusKind;
+typedef _PyLocalsPlusKind *_PyLocalsPlusKinds;
+
/* Bytecode object */
struct PyCodeObject {
PyObject_HEAD
@@ -53,9 +60,8 @@ struct PyCodeObject {
int co_kwonlyargcount; /* #keyword only arguments */
int co_stacksize; /* #entries needed for evaluation stack */
int co_firstlineno; /* first source line number */
- PyObject *co_varnames; /* tuple of strings (local variable names) */
- PyObject *co_cellvars; /* tuple of strings (cell variable names) */
- PyObject *co_freevars; /* tuple of strings (free variable names) */
+ PyObject *co_localsplusnames; /* tuple mapping offsets to names */
+ _PyLocalsPlusKinds co_localspluskinds; /* array mapping to local kinds */
PyObject *co_filename; /* unicode (where it was loaded from) */
PyObject *co_name; /* unicode (name, for reference) */
PyObject *co_linetable; /* string (encoding addr<->lineno mapping) See
@@ -65,11 +71,15 @@ struct PyCodeObject {
/* These fields are set with computed values on new code objects. */
int *co_cell2arg; /* Maps cell vars which are arguments. */
- // These are redundant but offer some performance benefit.
+ // redundant values (derived from co_localsplusnames and co_localspluskinds)
int co_nlocalsplus; /* number of local + cell + free variables */
int co_nlocals; /* number of local variables */
int co_ncellvars; /* number of cell variables */
int co_nfreevars; /* number of free variables */
+ // lazily-computed values
+ PyObject *co_varnames; /* tuple of strings (local variable names) */
+ PyObject *co_cellvars; /* tuple of strings (cell variable names) */
+ PyObject *co_freevars; /* tuple of strings (free variable names) */
/* The remaining fields are zeroed out on new code objects. */
@@ -143,7 +153,7 @@ struct PyCodeObject {
PyAPI_DATA(PyTypeObject) PyCode_Type;
#define PyCode_Check(op) Py_IS_TYPE(op, &PyCode_Type)
-#define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
+#define PyCode_GetNumFree(op) ((op)->co_nfreevars)
/* Public interface */
PyAPI_FUNC(PyCodeObject *) PyCode_New(