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.
8000 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 8000

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
Various small clarifications as suggested by Pablo.
  • Loading branch information
markshannon committed Jul 21, 2021
commit 90ed5b6b0fb1c46349e2701075433782417cbe75
13 changes: 9 additions & 4 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static inline int _PyFrameHasCompleted(InterpreterFrame *f) {

#define FRAME_SPECIALS_SIZE ((sizeof(InterpreterFrame)-1)/sizeof(PyObject *))

void _PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *locals);
void _PyFrame_TakeInterpreterFrame(PyFrameObject *f, InterpreterFrame *locals);

static inline void
_PyFrame_InitializeSpecials(
Expand Down Expand Up @@ -107,13 +107,13 @@ _PyFrame_GetFrameObject(InterpreterFrame *frame)
}

/* Clears all references in the frame.
* If take is non-zero, then the frame
* may be transfered to the frame object
* If take is non-zero, then the InterpreterFrame frame
* may be transfered to the frame object it references
* instead of being cleared. Either way
* the caller no longer owns the references
* in the frame.
* take should be set to 1 for heap allocated
* frames.
* frames like the ones in generators and coroutines.
*/
int
_PyFrame_Clear(InterpreterFrame * frame, int take);
Expand All @@ -124,6 +124,11 @@ _PyFrame_FastToLocalsWithError(InterpreterFrame *frame);
void
_PyFrame_LocalsToFast(InterpreterFrame *frame, int clear);

InterpreterFrame *_PyThreadState_PushFrame(
PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals);

void _PyThreadState_PopFrame(PyThreadState *tstate, InterpreterFrame *frame);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ PyAPI_FUNC(int) _PyState_AddModule(

PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);

struct _interpreter_frame *_PyThreadState_PushFrame(
PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals);

void _PyThreadState_PopFrame(PyThreadState *tstate, struct _interpreter_frame *frame);

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 6 additions & 3 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ frame_alloc(InterpreterFrame *frame, int owns)
}

void
_PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *frame)
_PyFrame_TakeInterpreterFrame(PyFrameObject *f, InterpreterFrame *frame)
{
assert(f->f_own_locals_memory == 0);
assert(frame->frame_obj == NULL);
Expand All @@ -866,17 +866,20 @@ _PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *frame)
f->f_frame = frame;
assert(f->f_back == NULL);
if (frame->previous != NULL) {
/* Link PyFrameObjects.f_back and remove link through InterpreterFrame.previous */
PyFrameObject *back = _PyFrame_GetFrameObject(frame->previous);
if (back == NULL) {
/* Memory error here. Nothing we can do about it */
/* Memory error here. */
assert(_PyErr_GetTopmostException(_PyThreadState_GET())->exc_type == PyExc_MemoryError);
/* Nothing we can do about it */
PyErr_Clear();
_PyErr_WriteUnraisableMsg("Out of memory lazily allocating frame->f_back", NULL);
}
else {
f->f_back = (PyFrameObject *)Py_NewRef(back);
}
frame->previous = NULL;
}
frame->previous = NULL;
if (!_PyObject_GC_IS_TRACKED((PyObject *)f)) {
_PyObject_GC_TRACK((PyObject *)f);
}
Expand Down
1 change: 1 addition & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8887,6 +8887,7 @@ super_init_without_args(PyFrameObject *f, PyCodeObject *co,
return -1;
}

assert(f->f_frame->nlocalsplus > 0);
PyObject *firstarg = _PyFrame_GetLocalsArray(f->f_frame)[0];
// The first argument might be a cell.
if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL)) {
Expand Down
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5162,7 +5162,7 @@ _PyFrame_Clear(InterpreterFrame * frame, int take)
return -1;
}
}
_PyFrame_TakeLocals(f, frame);
_PyFrame_TakeInterpreterFrame(f, frame);
Py_DECREF(f);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,7 @@ _PyThreadState_PushFrame(PyThreadState *tstate, PyFrameConstructor *con, PyObjec
{
PyCodeObject *code = (PyCodeObject *)con->fc_code;
int nlocalsplus = code->co_nlocalsplus;
int size = nlocalsplus + code->co_stacksize +
size_t size = nlocalsplus + code->co_stacksize +
FRAME_SPECIALS_SIZE;
PyObject **localsarray = tstate->datastack_top;
PyObject **top = localsarray + size;
Expand Down
0