summaryrefslogtreecommitdiffstats
path: root/Objects/clinic
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 /Objects/clinic
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 'Objects/clinic')
-rw-r--r--Objects/clinic/codeobject.c.h49
1 files changed, 48 insertions, 1 deletions
diff --git a/Objects/clinic/codeobject.c.h b/Objects/clinic/codeobject.c.h
index 7ffdf07..629d265 100644
--- a/Objects/clinic/codeobject.c.h
+++ b/Objects/clinic/codeobject.c.h
@@ -373,4 +373,51 @@ skip_optional_kwonly:
exit:
return return_value;
}
-/*[clinic end generated code: output=a272b22f63ea002e input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(code__varname_from_oparg__doc__,
+"_varname_from_oparg($self, /, oparg, *, cell=False)\n"
+"--\n"
+"\n"
+"(internal-only) Return the local variable name for the given oparg.\n"
+"\n"
+"WARNING: this method is for internal use only and may change or go away.");
+
+#define CODE__VARNAME_FROM_OPARG_METHODDEF \
+ {"_varname_from_oparg", (PyCFunction)(void(*)(void))code__varname_from_oparg, METH_FASTCALL|METH_KEYWORDS, code__varname_from_oparg__doc__},
+
+static PyObject *
+code__varname_from_oparg_impl(PyCodeObject *self, int oparg, int cell);
+
+static PyObject *
+code__varname_from_oparg(PyCodeObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ static const char * const _keywords[] = {"oparg", "cell", NULL};
+ static _PyArg_Parser _parser = {NULL, _keywords, "_varname_from_oparg", 0};
+ PyObject *argsbuf[2];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
+ int oparg;
+ int cell = 0;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ oparg = _PyLong_AsInt(args[0]);
+ if (oparg == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_kwonly;
+ }
+ cell = PyObject_IsTrue(args[1]);
+ if (cell < 0) {
+ goto exit;
+ }
+skip_optional_kwonly:
+ return_value = code__varname_from_oparg_impl(self, oparg, cell);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=43f4eef80d584fe0 input=a9049054013a1b77]*/