mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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
This commit is contained in:
parent
ea0210fa8c
commit
2c1e2583fd
21 changed files with 5971 additions and 5663 deletions
|
@ -1438,7 +1438,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
|
|||
|
||||
/* Local variable macros */
|
||||
|
||||
#define GETLOCAL(i) (fastlocals[i])
|
||||
#define GETLOCAL(i) (localsplus[i])
|
||||
|
||||
/* The SETLOCAL() macro must not DECREF the local variable in-place and
|
||||
then store the new value; it must copy the old value to a temporary
|
||||
|
@ -1569,7 +1569,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
|
|||
const _Py_CODEUNIT *next_instr;
|
||||
int opcode; /* Current opcode */
|
||||
int oparg; /* Current opcode argument, if any */
|
||||
PyObject **fastlocals, **freevars, **specials;
|
||||
PyObject **localsplus, **freevars, **specials;
|
||||
PyObject *retval = NULL; /* Return value */
|
||||
_Py_atomic_int * const eval_breaker = &tstate->interp->ceval.eval_breaker;
|
||||
PyCodeObject *co;
|
||||
|
@ -1646,7 +1646,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
|
|||
|
||||
names = co->co_names;
|
||||
consts = co->co_consts;
|
||||
fastlocals = f->f_localsptr;
|
||||
localsplus = f->f_localsptr;
|
||||
freevars = f->f_localsptr + co->co_nlocals;
|
||||
assert(PyBytes_Check(co->co_code));
|
||||
assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
|
||||
|
@ -1825,7 +1825,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
|
|||
if (value == NULL) {
|
||||
format_exc_check_arg(tstate, PyExc_UnboundLocalError,
|
||||
UNBOUNDLOCAL_ERROR_MSG,
|
||||
PyTuple_GetItem(co->co_varnames, oparg));
|
||||
PyTuple_GetItem(co->co_localsplusnames,
|
||||
oparg));
|
||||
goto error;
|
||||
}
|
||||
Py_INCREF(value);
|
||||
|
@ -3053,7 +3054,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
|
|||
format_exc_check_arg(
|
||||
tstate, PyExc_UnboundLocalError,
|
||||
UNBOUNDLOCAL_ERROR_MSG,
|
||||
PyTuple_GetItem(co->co_varnames, oparg)
|
||||
PyTuple_GetItem(co->co_localsplusnames, oparg)
|
||||
);
|
||||
goto error;
|
||||
}
|
||||
|
@ -3082,9 +3083,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
|
|||
Py_ssize_t idx;
|
||||
assert(locals);
|
||||
assert(oparg >= co->co_ncellvars);
|
||||
idx = oparg - co->co_ncellvars;
|
||||
assert(idx >= 0 && idx < co->co_nfreevars);
|
||||
name = PyTuple_GET_ITEM(co->co_freevars, idx);
|
||||
idx = oparg + co->co_nlocals;
|
||||
assert(idx < co->co_nlocalsplus);
|
||||
name = PyTuple_GET_ITEM(co->co_localsplusnames, idx);
|
||||
if (PyDict_CheckExact(locals)) {
|
||||
value = PyDict_GetItemWithError(locals, name);
|
||||
if (value != NULL) {
|
||||
|
@ -4636,7 +4637,7 @@ format_missing(PyThreadState *tstate, const char *kind,
|
|||
static void
|
||||
missing_arguments(PyThreadState *tstate, PyCodeObject *co,
|
||||
Py_ssize_t missing, Py_ssize_t defcount,
|
||||
PyObject **fastlocals, PyObject *qualname)
|
||||
PyObject **localsplus, PyObject *qualname)
|
||||
{
|
||||
Py_ssize_t i, j = 0;
|
||||
Py_ssize_t start, end;
|
||||
|
@ -4658,7 +4659,7 @@ missing_arguments(PyThreadState *tstate, PyCodeObject *co,
|
|||
}
|
||||
for (i = start; i < end; i++) {
|
||||
if (GETLOCAL(i) == NULL) {
|
||||
PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
|
||||
PyObject *raw = PyTuple_GET_ITEM(co->co_localsplusnames, i);
|
||||
PyObject *name = PyObject_Repr(raw);
|
||||
if (name == NULL) {
|
||||
Py_DECREF(missing_names);
|
||||
|
@ -4675,7 +4676,7 @@ missing_arguments(PyThreadState *tstate, PyCodeObject *co,
|
|||
static void
|
||||
too_many_positional(PyThreadState *tstate, PyCodeObject *co,
|
||||
Py_ssize_t given, PyObject *defaults,
|
||||
PyObject **fastlocals, PyObject *qualname)
|
||||
PyObject **localsplus, PyObject *qualname)
|
||||
{
|
||||
int plural;
|
||||
Py_ssize_t kwonly_given = 0;
|
||||
|
@ -4739,7 +4740,7 @@ positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
|
|||
PyObject* posonly_names = PyList_New(0);
|
||||
|
||||
for(int k=0; k < co->co_posonlyargcount; k++){
|
||||
PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
|
||||
PyObject* posonly_name = PyTuple_GET_ITEM(co->co_localsplusnames, k);
|
||||
|
||||
for (int k2=0; k2<kwcount; k2++){
|
||||
/* Compare the pointers first and fallback to PyObject_RichCompareBool*/
|
||||
|
@ -4876,12 +4877,12 @@ get_exception_handler(PyCodeObject *code, int index, int *level, int *handler, i
|
|||
|
||||
static int
|
||||
initialize_locals(PyThreadState *tstate, PyFrameConstructor *con,
|
||||
PyObject **fastlocals, PyObject *const *args,
|
||||
PyObject **localsplus, PyObject *const *args,
|
||||
Py_ssize_t argcount, PyObject *kwnames)
|
||||
{
|
||||
PyCodeObject *co = (PyCodeObject*)con->fc_code;
|
||||
const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
|
||||
PyObject **freevars = fastlocals + co->co_nlocals;
|
||||
PyObject **freevars = localsplus + co->co_nlocals;
|
||||
|
||||
/* Create a dictionary for keyword parameters (**kwags) */
|
||||
PyObject *kwdict;
|
||||
|
@ -4941,7 +4942,7 @@ initialize_locals(PyThreadState *tstate, PyFrameConstructor *con,
|
|||
|
||||
/* Speed hack: do raw pointer compares. As names are
|
||||
normally interned this should almost always hit. */
|
||||
co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
|
||||
co_varnames = ((PyTupleObject *)(co->co_localsplusnames))->ob_item;
|
||||
for (j = co->co_posonlyargcount; j < total_args; j++) {
|
||||
PyObject *varname = co_varnames[j];
|
||||
if (varname == keyword) {
|
||||
|
@ -4997,7 +4998,7 @@ initialize_locals(PyThreadState *tstate, PyFrameConstructor *con,
|
|||
|
||||
/* Check the number of positional arguments */
|
||||
if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
|
||||
too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
|
||||
too_many_positional(tstate, co, argcount, con->fc_defaults, localsplus,
|
||||
con->fc_qualname);
|
||||
goto fail;
|
||||
}
|
||||
|
@ -5013,7 +5014,7 @@ initialize_locals(PyThreadState *tstate, PyFrameConstructor *con,
|
|||
}
|
||||
}
|
||||
if (missing) {
|
||||
missing_arguments(tstate, co, missing, defcount, fastlocals,
|
||||
missing_arguments(tstate, co, missing, defcount, localsplus,
|
||||
con->fc_qualname);
|
||||
goto fail;
|
||||
}
|
||||
|
@ -5039,7 +5040,7 @@ initialize_locals(PyThreadState *tstate, PyFrameConstructor *con,
|
|||
for (i = co->co_argcount; i < total_args; i++) {
|
||||
if (GETLOCAL(i) != NULL)
|
||||
continue;
|
||||
PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
|
||||
PyObject *varname = PyTuple_GET_ITEM(co->co_localsplusnames, i);
|
||||
if (con->fc_kwdefaults != NULL) {
|
||||
PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
|
||||
if (def) {
|
||||
|
@ -5054,12 +5055,13 @@ initialize_locals(PyThreadState *tstate, PyFrameConstructor *con,
|
|||
missing++;
|
||||
}
|
||||
if (missing) {
|
||||
missing_arguments(tstate, co, missing, -1, fastlocals,
|
||||
missing_arguments(tstate, co, missing, -1, localsplus,
|
||||
con->fc_qualname);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Allocate and initialize storage for cell vars, and copy free
|
||||
vars into frame. */
|
||||
for (i = 0; i < co->co_ncellvars; ++i) {
|
||||
|
@ -6417,15 +6419,11 @@ format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
|
|||
/* Don't stomp existing exception */
|
||||
if (_PyErr_Occurred(tstate))
|
||||
return;
|
||||
name = PyTuple_GET_ITEM(co->co_localsplusnames, oparg + co->co_nlocals);
|
||||
if (oparg < co->co_ncellvars) {
|
||||
name = PyTuple_GET_ITEM(co->co_cellvars,
|
||||
oparg);
|
||||
format_exc_check_arg(tstate,
|
||||
PyExc_UnboundLocalError,
|
||||
UNBOUNDLOCAL_ERROR_MSG,
|
||||
name);
|
||||
format_exc_check_arg(tstate, PyExc_UnboundLocalError,
|
||||
UNBOUNDLOCAL_ERROR_MSG, name);
|
||||
} else {
|
||||
name = PyTuple_GET_ITEM(co->co_freevars, oparg - co->co_ncellvars);
|
||||
format_exc_check_arg(tstate, PyExc_NameError,
|
||||
UNBOUNDFREE_ERROR_MSG, name);
|
||||
}
|
||||
|
@ -6467,7 +6465,7 @@ unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
|
|||
switch (opcode) {
|
||||
case STORE_FAST:
|
||||
{
|
||||
PyObject **fastlocals = f->f_localsptr;
|
||||
PyObject **localsplus = f->f_localsptr;
|
||||
if (GETLOCAL(oparg) == v)
|
||||
SETLOCAL(oparg, NULL);
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue