Skip to content

GH-96421: Don't keep local copies of first_instr, names and consts in the interpreter. #96847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 25 additions & 34 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,13 @@ GETITEM(PyObject *v, Py_ssize_t i) {
/* Code access macros */

/* The integer overflow is checked by an assertion below. */
#define INSTR_OFFSET() ((int)(next_instr - first_instr))
#define INSTR_OFFSET() ((int)(next_instr - _PyCode_CODE(frame->f_code)))
#define NEXTOPARG() do { \
_Py_CODEUNIT word = *next_instr; \
opcode = _Py_OPCODE(word); \
oparg = _Py_OPARG(word); \
} while (0)
#define JUMPTO(x) (next_instr = first_instr + (x))
#define JUMPTO(x) (next_instr = _PyCode_CODE(frame->f_code) + (x))
#define JUMPBY(x) (next_instr += (x))

/* Get opcode and oparg from original instructions, not quickened form. */
Expand Down Expand Up @@ -1050,21 +1050,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int

/* Local "register" variables.
* These are cached values from the frame and code object. */

PyObject *names;
PyObject *consts;
_Py_CODEUNIT *first_instr;
_Py_CODEUNIT *next_instr;
PyObject **stack_pointer;

/* Sets the above local variables from the frame */
#define SET_LOCALS_FROM_FRAME() \
{ \
PyCodeObject *co = frame->f_code; \
names = co->co_names; \
consts = co->co_consts; \
first_instr = _PyCode_CODE(co); \
} \
assert(_PyInterpreterFrame_LASTI(frame) >= -1); \
/* Jump back to the last instruction executed... */ \
next_instr = frame->prev_instr + 1; \
Expand Down Expand Up @@ -1184,7 +1174,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int

TARGET(LOAD_CONST) {
PREDICTED(LOAD_CONST);
PyObject *value = GETITEM(consts, oparg);
PyObject *value = GETITEM(frame->f_code->co_consts, oparg);
Py_INCREF(value);
PUSH(value);
DISPATCH();
Expand Down Expand Up @@ -1217,7 +1207,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
next_instr++;
Py_INCREF(value);
PUSH(value);
value = GETITEM(consts, oparg);
value = GETITEM(frame->f_code->co_consts, oparg);
Py_INCREF(value);
PUSH(value);
DISPATCH();
Expand Down Expand Up @@ -1246,7 +1236,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(LOAD_CONST__LOAD_FAST) {
PyObject *value = GETITEM(consts, oparg);
PyObject *value = GETITEM(frame->f_code->co_consts, oparg);
NEXTOPARG();
next_instr++;
Py_INCREF(value);
Expand Down Expand Up @@ -2067,6 +2057,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
if (oparg) {
PyObject *lasti = PEEK(oparg + 1);
if (PyLong_Check(lasti)) {
_Py_CODEUNIT *first_instr = _PyCode_CODE(frame->f_code);
frame->prev_instr = first_instr + PyLong_AsLong(lasti);
assert(!_PyErr_Occurred(tstate));
}
Expand Down Expand Up @@ -2172,7 +2163,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(STORE_NAME) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *v = POP();
PyObject *ns = LOCALS();
int err;
Expand All @@ -2193,7 +2184,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(DELETE_NAME) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *ns = LOCALS();
int err;
if (ns == NULL) {
Expand Down Expand Up @@ -2298,7 +2289,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int

TARGET(STORE_ATTR) {
PREDICTED(STORE_ATTR);
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *owner = TOP();
PyObject *v = SECOND();
int err;
Expand All @@ -2314,7 +2305,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(DELETE_ATTR) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *owner = POP();
int err;
err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
Expand All @@ -2325,7 +2316,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(STORE_GLOBAL) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *v = POP();
int err;
err = PyDict_SetItem(GLOBALS(), name, v);
Expand All @@ -2336,7 +2327,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(DELETE_GLOBAL) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
int err;
err = PyDict_DelItem(GLOBALS(), name);
if (err != 0) {
Expand All @@ -2350,7 +2341,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(LOAD_NAME) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *locals = LOCALS();
PyObject *v;
if (locals == NULL) {
Expand Down Expand Up @@ -2417,7 +2408,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
PREDICTED(LOAD_GLOBAL);
int push_null = oparg & 1;
PEEK(0) = NULL;
PyObject *name = GETITEM(names, oparg>>1);
PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1);
PyObject *v;
if (PyDict_CheckExact(GLOBALS())
&& PyDict_CheckExact(BUILTINS()))
Expand Down Expand Up @@ -2470,7 +2461,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
assert(cframe.use_tracing == 0);
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache)) {
PyObject *name = GETITEM(names, oparg>>1);
PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1);
next_instr--;
if (_Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name) < 0) {
goto error;
Expand Down Expand Up @@ -2876,7 +2867,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int

TARGET(LOAD_ATTR) {
PREDICTED(LOAD_ATTR);
PyObject *name = GETITEM(names, oparg >> 1);
PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1);
PyObject *owner = TOP();
if (oparg & 1) {
/* Designed to work in tandem with CALL. */
Expand Down Expand Up @@ -2928,7 +2919,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
_PyAttrCache *cache = (_PyAttrCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache)) {
PyObject *owner = TOP();
PyObject *name = GETITEM(names, oparg>>1);
PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1);
next_instr--;
if (_Py_Specialize_LoadAttr(owner, next_instr, name) < 0) {
goto error;
Expand Down Expand Up @@ -3007,7 +2998,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
DEOPT_IF(dict == NULL, LOAD_ATTR);
assert(PyDict_CheckExact((PyObject *)dict));
PyObject *name = GETITEM(names, oparg>>1);
PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1);
uint16_t hint = cache->index;
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, LOAD_ATTR);
if (DK_IS_UNICODE(dict->ma_keys)) {
Expand Down Expand Up @@ -3131,7 +3122,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), CALL);
STAT_INC(LOAD_ATTR, hit);

PyObject *name = GETITEM(names, oparg >> 1);
PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1);
Py_INCREF(f);
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f);
SET_TOP(NULL);
Expand All @@ -3157,7 +3148,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
_PyAttrCache *cache = (_PyAttrCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache)) {
PyObject *owner = TOP();
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
next_instr--;
if (_Py_Specialize_StoreAttr(owner, next_instr, name) < 0) {
goto error;
Expand Down Expand Up @@ -3214,7 +3205,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv);
DEOPT_IF(dict == NULL, STORE_ATTR);
assert(PyDict_CheckExact((PyObject *)dict));
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
uint16_t hint = cache->index;
DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, STORE_ATTR);
PyObject *value, *old_value;
Expand Down Expand Up @@ -3482,7 +3473,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(IMPORT_NAME) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *fromlist = POP();
PyObject *level = TOP();
PyObject *res;
Expand Down Expand Up @@ -3519,7 +3510,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}

TARGET(IMPORT_FROM) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
PyObject *from = TOP();
PyObject *res;
res = import_from(tstate, from, name);
Expand Down Expand Up @@ -4109,8 +4100,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int

TARGET(KW_NAMES) {
assert(call_shape.kwnames == NULL);
assert(oparg < PyTuple_GET_SIZE(consts));
call_shape.kwnames = GETITEM(consts, oparg);
assert(oparg < PyTuple_GET_SIZE(frame->f_code->co_consts));
call_shape.kwnames = GETITEM(frame->f_code->co_consts, oparg);
DISPATCH();
}

Expand Down