FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143421: Lazily allocate tracer code and opt buffers by Fidget-Spinner · Pull Request #143597 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (3) .h  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions Include/internal/pycore_optimizer_types.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extern "C" {

#include "pycore_uop.h" // UOP_MAX_TRACE_LENGTH

// Holds locals, stack, locals, stack ... co_consts (in that order)
#define MAX_ABSTRACT_INTERP_SIZE 4096
// Holds locals, stack, locals, stack ... (in that order)
#define MAX_ABSTRACT_INTERP_SIZE 512

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This number was picked arbitrarily by me back then when I first wrote this. I now realise it's overkill. We don't need 3 pages of memory just to store locals and stack. It's slow and wastes memory.


#define TY_ARENA_SIZE (UOP_MAX_TRACE_LENGTH * 5)

Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_tstate.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ typedef struct _PyJitTracerState {
_PyJitTracerInitialState initial_state;
_PyJitTracerPreviousState prev_state;
_PyJitTracerTranslatorState translator_state;
JitOptContext opt_context;
_PyUOpInstruction code_buffer[UOP_MAX_TRACE_LENGTH];
JitOptContext *opt_context;
_PyUOpInstruction *code_buffer;
} _PyJitTracerState;

#endif
Expand Down
7 changes: 7 additions & 0 deletions Python/optimizer.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,13 @@ _PyJit_TryInitializeTracing(
if (oparg > 0xFFFF) {
return 0;
}
if (_tstate->jit_tracer_state.code_buffer == NULL) {
_tstate->jit_tracer_state.code_buffer = (_PyUOpInstruction *)_PyObject_VirtualAlloc(UOP_BUFFER_SIZE);
if (_tstate->jit_tracer_state.code_buffer == NULL) {
// Don't error, just go to next instruction.
return 0;
}
}
PyObject *func = PyStackRef_AsPyObjectBorrow(frame->f_funcobj);
if (func == NULL) {
return 0;
Expand Down
10 changes: 9 additions & 1 deletion Python/optimizer_analysis.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,15 @@ optimize_uops(
assert(!PyErr_Occurred());
PyFunctionObject *func = tstate->jit_tracer_state.initial_state.func;

JitOptContext *ctx = &tstate->jit_tracer_state.opt_context;
JitOptContext *ctx = tstate->jit_tracer_state.opt_context;
if (ctx == NULL) {
ctx = (JitOptContext *)_PyObject_VirtualAlloc(sizeof(JitOptContext));
if (ctx == NULL) {
// Don't error, just bail.
return 0;
}
tstate->jit_tracer_state.opt_context = ctx;
}
uint32_t opcode = UINT16_MAX;

// Make sure that watchers are set up
Expand Down
14 changes: 14 additions & 0 deletions Python/pystate.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,8 @@ init_threadstate(_PyThreadStateImpl *_tstate,
init_policy(&_tstate->policy.jit.side_exit_initial_backoff,
"PYTHON_JIT_SIDE_EXIT_INITIAL_BACKOFF",
SIDE_EXIT_INITIAL_BACKOFF, 0, MAX_BACKOFF);
_tstate->jit_tracer_state.code_buffer = NULL;
_tstate->jit_tracer_state.opt_context = NULL;
#endif
tstate->delete_later = NULL;

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

#if _Py_TIER2
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
if (_tstate->jit_tracer_state.code_buffer != NULL) {
_PyObject_VirtualFree(_tstate->jit_tracer_state.code_buffer, UOP_BUFFER_SIZE);
_tstate->jit_tracer_state.code_buffer = NULL;
}
if (_tstate->jit_tracer_state.opt_context != NULL) {
_PyObject_VirtualFree(_tstate->jit_tracer_state.opt_context, sizeof(JitOptContext));
_tstate->jit_tracer_state.opt_context = NULL;
}
#endif

HEAD_UNLOCK(runtime);

// XXX Unbind in PyThreadState_Clear(), or earlier
Expand Down
Loading

Back | FazBrowse Home | New Git URL