8000 bpo-44590: Lazily allocate frame objects by markshannon · Pull Request #27077 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
79aeaf6
Turn specials array into struct and add 'lasti' to it.
markshannon Jun 21, 2021
3b6a4e8
Move stack-depth from frame object to specials struct.
markshannon Jun 22, 2021
6f0ba40
Rename 'specials' to 'frame' as it now includes most of the data for …
markshannon Jun 22, 2021
8543c27
Refactor, pushing PyFrame upward toward _PyEval_Vector.
markshannon Jun 22, 2021
423d403
Add pointer from stack frame to frame object and rename tstate.frame …
markshannon Jun 22, 2021
7bf6c30
More refactoring. Add tstate.frame for the top stack frame.
markshannon Jun 23, 2021
861a8d9
Convert use of PyFrameObject to _PyFrame.
markshannon Jun 23, 2021
35e793c
Replace most remaining uses frame object in the interpreter with stac…
markshannon Jun 24, 2021
192094e
Convert more uses of frameobject to frame.
markshannon Jun 24, 2021
3f601a7
Move f_state from frame object to frame.
markshannon Jun 29, 2021
bd95c32
Compute f_back when on thread stack, only filling in value when frame…
markshannon Jun 30, 2021
9961abc
Add NULL check
markshannon Jun 30, 2021
32af707
Get lazy f_back working (it still leaks).
markshannon Jul 2, 2021
ac7dbe8
Use frames not frameobjects in sys._getframe()
markshannon Jul 2, 2021
f33d291
NULL out frame->previous when leaving frame.
markshannon Jul 2, 2021
5c23a36
Frames now include nlocalspuls, so they have valid layout after code …
markshannon Jul 5, 2021
910e991
Move ownership of frame in generator from frame object ot generator o…
markshannon Jul 5, 2021
22e1c9b
Remove localsptr field from frame object.
markshannon Jul 6, 2021
f84a3f0
Add new _PyEval_EvalNoFrame function for evaluating frames directly.
markshannon Jul 6, 2021
1180a44
Allow for lazily created frames.
markshannon Jul 6, 2021
c76de89
Do not create frame objects for Python calls.
markshannon Jul 6, 2021
15aeef1
Don't create frame objects for generators.
markshannon Jul 6, 2021
1d2e1ce
Fix memory leak
markshannon Jul 7, 2021
1b19f8b
Merge branch 'main' into lazy-frame-updated
markshannon Jul 7, 2021
d619bae
Restore support for PEP 523.
markshannon Jul 8, 2021
d147d03
Streamline pushing and popping stack frames a bit.
markshannon Jul 9, 2021
25c6a71
Merge branch 'main' into lazy-frame
markshannon Jul 9, 2021
618b094
Add f_ prefix back to several frame fields to ease porting C code tha…
markshannon Jul 9, 2021
e5da338
Add NEWS
markshannon Jul 9, 2021
dda0b0c
Remove debugging artifact.
markshannon Jul 9, 2021
5ecc067
Make symbol private
markshannon Jul 9, 2021
3b65a0f
Fix use-after-free error.
markshannon Jul 9, 2021
596213d
Add some explanatory comments.
markshannon Jul 9, 2021
386275e
Remove debugging artifact.
markshannon Jul 9, 2021
596c041
Merge branch 'main' into lazy-frame
markshannon Jul 15, 2021
039bca7
Rename _PyFrame to InterpreterFrame.
markshannon Jul 15, 2021
2cad33b
Remove use-after-free in assert.
markshannon Jul 19, 2021
77cf187
Merge branch 'main' into lazy-frame
markshannon Jul 19, 2021
decf209
Make name of frame argument consistent across _PyEval_Vector, _PyEval…
markshannon Jul 19, 2021
666b618
Allow for old gdbs still using Python 2.
markshannon Jul 19, 2021
90ed5b6
Various small clarifications as suggested by Pablo.
markshannon Jul 21, 2021
593a348
Refactor interpreter frame code into its own file. Improve a few names.
markshannon Jul 21, 2021
b775f13
Tidy up assert.
markshannon Jul 21, 2021
e8476b2
Fix warning on Windows.
markshannon Jul 21, 2021
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
Prev Previous commit
Next Next commit
Convert more uses of frameobject to frame.
  • Loading branch information
markshannon committed Jun 24, 2021
commit 192094ef81fcbd0ad849fecdc9663b644bef2723
6 changes: 6 additions & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ _PyFrame_GetFrameObject(_PyFrame *frame)
return frame->frame_obj;
}

int
_PyFrame_FastToLocalsWithError(_PyFrame *frame, int cleared);

void
_PyFrame_LocalsToFast(_PyFrame *frame, int clear);

#ifdef __cplusplus
}
#endif
Expand Down
58 changes: 34 additions & 24 deletions Objects/frameobject.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,11 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
}

static int
_PyFrame_OpAlreadyRan(PyFrameObject *f, int opcode, int oparg)
_PyFrame_OpAlreadyRan(_PyFrame *frame, int opcode, int oparg)
{
const _Py_CODEUNIT *code =
(const _Py_CODEUNIT *)PyBytes_AS_STRING(f->f_frame->code->co_code);
for (int i = 0; i < f->f_frame->lasti; i++) {
(const _Py_CODEUNIT *)PyBytes_AS_STRING(frame->code->co_code);
for (int i = 0; i < frame->lasti; i++) {
if (_Py_OPCODE(code[i]) == opcode && _Py_OPARG(code[i]) == oparg) {
return 1;
}
Expand All @@ -928,25 +928,19 @@ _PyFrame_OpAlreadyRan(PyFrameObject *f, int opcode, int oparg)
}

int
PyFrame_FastToLocalsWithError(PyFrameObject *f)
{
_PyFrame_FastToLocalsWithError(_PyFrame *frame, int cleared) {
/* Merge fast locals into f->f_locals */
PyObject *locals;
PyObject **fast;
PyCodeObject *co;

if (f == NULL) {
PyErr_BadInternalCall();
return -1;
}
locals = f->f_frame->locals;
locals = frame->locals;
if (locals == NULL) {
locals = f->f_frame->locals = PyDict_New();
locals = frame->locals = PyDict_New();
if (locals == NULL)
return -1;
}
co = f->f_frame->code;
fast = f->f_localsptr;
co = frame->code;
fast = _PyFrame_GetLocalsArray(frame);
for (int i = 0; i < co->co_nlocalsplus; i++) {
_PyLocalsPlusKind kind = co->co_localspluskinds[i];

Expand All @@ -964,7 +958,7 @@ PyFrame_FastToLocalsWithError(PyFrameObject *f)

PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
PyObject *value = fast[i];
if (f->f_state != FRAME_CLEARED) {
if (!cleared) {
if (kind & CO_FAST_FREE) {
// The cell was set when the frame was created from
// the function's closure.
Expand All @@ -978,7 +972,7 @@ PyFrame_FastToLocalsWithError(PyFrameObject *f)
// run yet.
if (value != NULL) {
if (PyCell_Check(value) &&
_PyFrame_OpAlreadyRan(f, MAKE_CELL, i)) {
_PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
// (likely) MAKE_CELL must have executed already.
value = PyCell_GET(value);
}
Expand Down Expand Up @@ -1011,6 +1005,16 @@ PyFrame_FastToLocalsWithError(PyFrameObject *f)
return 0;
}

int
PyFrame_FastToLocalsWithError(PyFrameObject *f)
{
if (f == NULL) {
PyErr_BadInternalCall();
return -1;
}
return _PyFrame_FastToLocalsWithError(f->f_frame, f->f_state == FRAME_CLEARED);
}

void
PyFrame_FastToLocals(PyFrameObject *f)
{
Expand All @@ -1024,21 +1028,18 @@ PyFrame_FastToLocals(PyFrameObject *f)
}

void
PyFrame_LocalsToFast(PyFrameObject *f, int clear)
_PyFrame_LocalsToFast(_PyFrame *frame, int clear)
{
/* Merge locals into fast locals */
PyObject *locals;
PyObject **fast;
PyObject *error_type, *error_value, *error_traceback;
PyCodeObject *co;
if (f == NULL || f->f_state == FRAME_CLEARED) {
return;
}
locals = f->f_frame->locals;
locals = frame->locals;
if (locals == NULL)
return;
fast = f->f_localsptr;
co = f->f_frame->code;
fast = _PyFrame_GetLocalsArray(frame);
co = frame->code;

PyErr_Fetch(&error_type, &error_value, &error_traceback);
for (int i = 0; i < co->co_nlocalsplus; i++) {
Expand Down Expand Up @@ -1068,7 +1069,7 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear)
else if (kind & CO_FAST_CELL && oldvalue != NULL) {
/* Same test as in PyFrame_FastToLocals() above. */
if (PyCell_Check(oldvalue) &&
_PyFrame_OpAlreadyRan(f, MAKE_CELL, i)) {
_PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
// (likely) MAKE_CELL must have executed already.
cell = oldvalue;
}
Expand All @@ -1092,6 +1093,15 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear)
PyErr_Restore(error_type, error_value, error_traceback);
}

void
PyFrame_LocalsToFast(PyFrameObject *f, int clear)
{
if (f == NULL || f->f_state == FRAME_CLEARED) {
return;
}
_PyFrame_LocalsToFast(f->f_frame, clear);
}

/* Clear out the free list */
void
_PyFrame_ClearFreeList(PyInterpreterState *interp)
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3507,7 +3507,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *fo, int throwflag
case TARGET(IMPORT_STAR): {
PyObject *from = POP(), *locals;
int err;
if (PyFrame_FastToLocalsWithError(fo) < 0) {
if (_PyFrame_FastToLocalsWithError(frame, 0) < 0) {
Py_DECREF(from);
goto error;
}
Expand All @@ -3520,7 +3520,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *fo, int throwflag
goto error;
}
err = import_all_from(tstate, locals, from);
PyFrame_LocalsToFast(fo, 0);
_PyFrame_LocalsToFast(frame, 0);
Py_DECREF(from);
if (err != 0)
goto error;
Expand Down
0