Skip to content

Commit 5b4bcb0

Browse files
committed
TOS caching
1 parent 68e72cf commit 5b4bcb0

File tree

11 files changed

+2767
-701
lines changed

11 files changed

+2767
-701
lines changed

Include/internal/pycore_interpframe.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static inline PyFunctionObject *_PyFrame_GetFunction(_PyInterpreterFrame *f) {
4343
}
4444

4545
static inline _PyStackRef *_PyFrame_Stackbase(_PyInterpreterFrame *f) {
46-
return (f->localsplus + _PyFrame_GetCode(f)->co_nlocalsplus);
46+
return (f->localsplus + _PyFrame_GetCode(f)->co_nlocalsplus + 1);
4747
}
4848

4949
static inline _PyStackRef _PyFrame_StackPeek(_PyInterpreterFrame *f) {
@@ -134,7 +134,7 @@ _PyFrame_Initialize(
134134
frame->f_builtins = func_obj->func_builtins;
135135
frame->f_globals = func_obj->func_globals;
136136
frame->f_locals = locals;
137-
frame->stackpointer = frame->localsplus + code->co_nlocalsplus;
137+
frame->stackpointer = frame->localsplus + code->co_nlocalsplus + 1;
138138
frame->frame_obj = NULL;
139139
#ifdef Py_GIL_DISABLED
140140
_PyFrame_InitializeTLBC(tstate, frame, code);
@@ -149,7 +149,7 @@ _PyFrame_Initialize(
149149
frame->lltrace = 0;
150150
#endif
151151

152-
for (int i = null_locals_from; i < code->co_nlocalsplus; i++) {
152+
for (int i = null_locals_from; i <= code->co_nlocalsplus; i++) {
153153
frame->localsplus[i] = PyStackRef_NULL;
154154
}
155155
}
@@ -317,7 +317,7 @@ _PyFrame_PushTrampolineUnchecked(PyThreadState *tstate, PyCodeObject *code, int
317317
#endif
318318
frame->f_locals = NULL;
319319
assert(stackdepth <= code->co_stacksize);
320-
frame->stackpointer = frame->localsplus + code->co_nlocalsplus + stackdepth;
320+
frame->stackpointer = frame->localsplus + code->co_nlocalsplus + stackdepth + 1;
321321
frame->frame_obj = NULL;
322322
#ifdef Py_GIL_DISABLED
323323
_PyFrame_InitializeTLBC(tstate, frame, code);

Lib/test/test_sys.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,9 +1666,9 @@ def func():
16661666
return sys._getframe()
16671667
x = func()
16681668
if support.Py_GIL_DISABLED:
1669-
INTERPRETER_FRAME = '9PihcP'
1669+
INTERPRETER_FRAME = '9PihcPP'
16701670
else:
1671-
INTERPRETER_FRAME = '9PhcP'
1671+
INTERPRETER_FRAME = '9PhcPP'
16721672
check(x, size('3PiccPPP' + INTERPRETER_FRAME + 'P'))
16731673
# function
16741674
def func(): pass

Objects/codeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
546546
/* derived values */
547547
co->co_nlocalsplus = nlocalsplus;
548548
co->co_nlocals = nlocals;
549-
co->co_framesize = nlocalsplus + con->stacksize + FRAME_SPECIALS_SIZE;
549+
co->co_framesize = nlocalsplus + con->stacksize + FRAME_SPECIALS_SIZE + 1;
550550
co->co_ncellvars = ncellvars;
551551
co->co_nfreevars = nfreevars;
552552
#ifdef Py_GIL_DISABLED

Python/bytecodes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3995,8 +3995,9 @@ dummy_func(
39953995
tstate, (PyCodeObject *)&_Py_InitCleanup, 1, frame);
39963996
assert(_PyFrame_GetBytecode(shim)[0].op.code == EXIT_INIT_CHECK);
39973997
assert(_PyFrame_GetBytecode(shim)[1].op.code == RETURN_VALUE);
3998+
shim->localsplus[0] = PyStackRef_NULL;
39983999
/* Push self onto stack of shim */
3999-
shim->localsplus[0] = PyStackRef_DUP(self[0]);
4000+
shim->localsplus[1] = PyStackRef_DUP(self[0]);
40004001
_PyInterpreterFrame *temp = _PyEvalFramePushAndInit(
40014002
tstate, init[0], NULL, args-1, oparg+1, NULL, shim);
40024003
DEAD(init);

Python/ceval.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,17 @@ dump_stack(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer)
190190

191191
static void
192192
lltrace_instruction(_PyInterpreterFrame *frame,
193+
_PyStackRef _tos,
193194
_PyStackRef *stack_pointer,
194195
_Py_CODEUNIT *next_instr,
195196
int opcode,
196197
int oparg)
197198
{
198199
int offset = 0;
199200
if (frame->owner < FRAME_OWNED_BY_INTERPRETER) {
201+
printf("_tos = ");
202+
dump_item(_tos);
203+
printf("; ");
200204
dump_stack(frame, stack_pointer);
201205
offset = (int)(next_instr - _PyFrame_GetBytecode(frame));
202206
}
@@ -943,6 +947,13 @@ _PyObjectArray_Free(PyObject **array, PyObject **scratch)
943947
/* This setting is reversed below following _PyEval_EvalFrameDefault */
944948
#endif
945949

950+
#ifdef Py_GIL_DISABLED
951+
#define Py_FREE_THREADING 1
952+
#else
953+
#define Py_FREE_THREADING 0
954+
#endif
955+
956+
946957
#if Py_TAIL_CALL_INTERP
947958
#include "opcode_targets.h"
948959
#include "generated_cases.c.h"
@@ -992,7 +1003,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
9921003
#endif
9931004
entry_frame.f_executable = PyStackRef_None;
9941005
entry_frame.instr_ptr = (_Py_CODEUNIT *)_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS + 1;
995-
entry_frame.stackpointer = entry_frame.localsplus;
1006+
entry_frame.stackpointer = entry_frame.localsplus + 1;
9961007
entry_frame.owner = FRAME_OWNED_BY_INTERPRETER;
9971008
entry_frame.visited = 0;
9981009
entry_frame.return_offset = 0;
@@ -1039,6 +1050,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
10391050
_PyExecutorObject *current_executor = NULL;
10401051
const _PyUOpInstruction *next_uop = NULL;
10411052
#endif
1053+
_PyStackRef _tos = PyStackRef_NULL;
10421054
#if Py_TAIL_CALL_INTERP
10431055
return _TAIL_CALL_start_frame(frame, NULL, tstate, NULL, 0);
10441056
#else

Python/ceval_macros.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
/* PRE_DISPATCH_GOTO() does lltrace if enabled. Normally a no-op */
111111
#ifdef Py_DEBUG
112112
#define PRE_DISPATCH_GOTO() if (frame->lltrace >= 5) { \
113-
lltrace_instruction(frame, stack_pointer, next_instr, opcode, oparg); }
113+
lltrace_instruction(frame, _tos, stack_pointer, next_instr, opcode, oparg); }
114114
#else
115115
#define PRE_DISPATCH_GOTO() ((void)0)
116116
#endif
@@ -198,7 +198,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
198198
#define STACK_SIZE() (_PyFrame_GetCode(frame)->co_stacksize)
199199

200200
#define WITHIN_STACK_BOUNDS() \
201-
(frame->owner == FRAME_OWNED_BY_INTERPRETER || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
201+
(frame->owner == FRAME_OWNED_BY_INTERPRETER || (STACK_LEVEL() >= -1 && STACK_LEVEL() <= STACK_SIZE()))
202202

203203
/* Data access macros */
204204
#define FRAME_CO_CONSTS (_PyFrame_GetCode(frame)->co_consts)

Python/executor_cases.c.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)