Skip to content

Commit b852236

Browse files
gh-143421: Lazily allocate tracer code and opt buffers (GH-143597)
1 parent 61e0366 commit b852236

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

Include/internal/pycore_optimizer_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ extern "C" {
1010

1111
#include "pycore_uop.h" // UOP_MAX_TRACE_LENGTH
1212

13-
// Holds locals, stack, locals, stack ... co_consts (in that order)
14-
#define MAX_ABSTRACT_INTERP_SIZE 4096
13+
// Holds locals, stack, locals, stack ... (in that order)
14+
#define MAX_ABSTRACT_INTERP_SIZE 512
1515

1616
#define TY_ARENA_SIZE (UOP_MAX_TRACE_LENGTH * 5)
1717

Include/internal/pycore_tstate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ typedef struct _PyJitTracerState {
5656
_PyJitTracerInitialState initial_state;
5757
_PyJitTracerPreviousState prev_state;
5858
_PyJitTracerTranslatorState translator_state;
59-
JitOptContext opt_context;
60-
_PyUOpInstruction code_buffer[UOP_MAX_TRACE_LENGTH];
59+
JitOptContext *opt_context;
60+
_PyUOpInstruction *code_buffer;
6161
} _PyJitTracerState;
6262

6363
#endif

Python/optimizer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,13 @@ _PyJit_TryInitializeTracing(
10251025
if (oparg > 0xFFFF) {
10261026
return 0;
10271027
}
1028+
if (_tstate->jit_tracer_state.code_buffer == NULL) {
1029+
_tstate->jit_tracer_state.code_buffer = (_PyUOpInstruction *)_PyObject_VirtualAlloc(UOP_BUFFER_SIZE);
1030+
if (_tstate->jit_tracer_state.code_buffer == NULL) {
1031+
// Don't error, just go to next instruction.
1032+
return 0;
1033+
}
1034+
}
10281035
PyObject *func = PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
10291036
if (func == NULL) {
10301037
return 0;

Python/optimizer_analysis.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,15 @@ optimize_uops(
345345
assert(!PyErr_Occurred());
346346
PyFunctionObject *func = tstate->jit_tracer_state.initial_state.func;
347347

348-
JitOptContext *ctx = &tstate->jit_tracer_state.opt_context;
348+
JitOptContext *ctx = tstate->jit_tracer_state.opt_context;
349+
if (ctx == NULL) {
350+
ctx = (JitOptContext *)_PyObject_VirtualAlloc(sizeof(JitOptContext));
351+
if (ctx == NULL) {
352+
// Don't error, just bail.
353+
return 0;
354+
}
355+
tstate->jit_tracer_state.opt_context = ctx;
356+
}
349357
uint32_t opcode = UINT16_MAX;
350358

351359
// Make sure that watchers are set up

Python/pystate.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,8 @@ init_threadstate(_PyThreadStateImpl *_tstate,
15531553
init_policy(&_tstate->policy.jit.side_exit_initial_backoff,
15541554
"PYTHON_JIT_SIDE_EXIT_INITIAL_BACKOFF",
15551555
SIDE_EXIT_INITIAL_BACKOFF, 0, MAX_BACKOFF);
1556+
_tstate->jit_tracer_state.code_buffer = NULL;
1557+
_tstate->jit_tracer_state.opt_context = NULL;
15561558
#endif
15571559
tstate->delete_later = NULL;
15581560

@@ -1867,6 +1869,18 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
18671869
assert(tstate_impl->refcounts.values == NULL);
18681870
#endif
18691871

1872+
#if _Py_TIER2
1873+
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
1874+
if (_tstate->jit_tracer_state.code_buffer != NULL) {
1875+
_PyObject_VirtualFree(_tstate->jit_tracer_state.code_buffer, UOP_BUFFER_SIZE);
1876+
_tstate->jit_tracer_state.code_buffer = NULL;
1877+
}
1878+
if (_tstate->jit_tracer_state.opt_context != NULL) {
1879+
_PyObject_VirtualFree(_tstate->jit_tracer_state.opt_context, sizeof(JitOptContext));
1880+
_tstate->jit_tracer_state.opt_context = NULL;
1881+
}
1882+
#endif
1883+
18701884
HEAD_UNLOCK(runtime);
18711885

18721886
// XXX Unbind in PyThreadState_Clear(), or earlier

0 commit comments

Comments
 (0)