summaryrefslogtreecommitdiffstats
path: root/Python/marshal.c
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 /Python/marshal.c
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 'Python/marshal.c')
-rw-r--r--Python/marshal.c52
1 files changed, 30 insertions, 22 deletions
diff --git a/Python/marshal.c b/Python/marshal.c
index 52bf2a5..80517b3 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -518,9 +518,8 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
w_object(co->co_code, p);
w_object(co->co_consts, p);
w_object(co->co_names, p);
- w_object(co->co_varnames, p);
- w_object(co->co_freevars, p);
- w_object(co->co_cellvars, p);
+ w_object(co->co_localsplusnames, p);
+ w_string(co->co_localspluskinds, co->co_nlocalsplus, p);
w_object(co->co_filename, p);
w_object(co->co_name, p);
w_long(co->co_firstlineno, p);
@@ -1306,9 +1305,8 @@ r_object(RFILE *p)
PyObject *code = NULL;
PyObject *consts = NULL;
PyObject *names = NULL;
- PyObject *varnames = NULL;
- PyObject *freevars = NULL;
- PyObject *cellvars = NULL;
+ PyObject *localsplusnames = NULL;
+ _PyLocalsPlusKinds localspluskinds = NULL;
PyObject *filename = NULL;
PyObject *name = NULL;
int firstlineno;
@@ -1347,16 +1345,22 @@ r_object(RFILE *p)
names = r_object(p);
if (names == NULL)
goto code_error;
- varnames = r_object(p);
- if (varnames == NULL)
- goto code_error;
- Py_ssize_t nlocals = PyTuple_GET_SIZE(varnames);
- freevars = r_object(p);
- if (freevars == NULL)
- goto code_error;
- cellvars = r_object(p);
- if (cellvars == NULL)
+ localsplusnames = r_object(p);
+ if (localsplusnames == NULL)
goto code_error;
+
+ assert(PyTuple_GET_SIZE(localsplusnames) < INT_MAX);
+ int nlocalsplus = (int)PyTuple_GET_SIZE(localsplusnames);
+ if (nlocalsplus) {
+ if (_PyCode_InitLocalsPlusKinds(nlocalsplus,
+ &localspluskinds) < 0) {
+ goto code_error;
+ }
+ for (int i = 0; i < nlocalsplus; i++) {
+ localspluskinds[i] = r_byte(p);
+ }
+ }
+
filename = r_object(p);
if (filename == NULL)
goto code_error;
@@ -1375,7 +1379,8 @@ r_object(RFILE *p)
if (PySys_Audit("code.__new__", "OOOiiiiii",
code, filename, name, argcount, posonlyargcount,
- kwonlyargcount, nlocals, stacksize, flags) < 0) {
+ kwonlyargcount, nlocalsplus, stacksize,
+ flags) < 0) {
goto code_error;
}
@@ -1391,9 +1396,8 @@ r_object(RFILE *p)
.consts = consts,
.names = names,
- .varnames = varnames,
- .cellvars = cellvars,
- .freevars = freevars,
+ .localsplusnames = localsplusnames,
+ .localspluskinds = localspluskinds,
.argcount = argcount,
.posonlyargcount = posonlyargcount,
@@ -1403,22 +1407,26 @@ r_object(RFILE *p)
.exceptiontable = exceptiontable,
};
+
if (_PyCode_Validate(&con) < 0) {
goto code_error;
}
+
v = (PyObject *)_PyCode_New(&con);
if (v == NULL) {
goto code_error;
}
+
+ localspluskinds = NULL; // This keeps it from getting freed below.
+
v = r_ref_insert(v, idx, flag, p);
code_error:
Py_XDECREF(code);
Py_XDECREF(consts);
Py_XDECREF(names);
- Py_XDECREF(varnames);
- Py_XDECREF(freevars);
- Py_XDECREF(cellvars);
+ Py_XDECREF(localsplusnames);
+ _PyCode_ClearLocalsPlusKinds(localspluskinds);
Py_XDECREF(filename);
Py_XDECREF(name);
Py_XDECREF(linetable);