Skip to content

Commit d12b705

Browse files
committed
Use lazy allocation
1 parent 471832b commit d12b705

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Include/internal/pycore_tstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ typedef struct _PyThreadStateImpl {
7878
#endif
7979

8080
#ifdef _Py_TIER2
81-
struct _PyUOpInstruction buffer[UOP_MAX_TRACE_LENGTH];
81+
struct _PyUOpInstruction *buffer;
8282
#endif
8383

8484
} _PyThreadStateImpl;

Python/optimizer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,13 @@ uop_optimize(
12811281
_PyBloomFilter dependencies;
12821282
_Py_BloomFilter_Init(&dependencies);
12831283
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
1284+
if (tstate->buffer == NULL) {
1285+
tstate->buffer = (_PyUOpInstruction *)PyMem_RawMalloc(UOP_MAX_TRACE_LENGTH*sizeof(_PyUOpInstruction));
1286+
if (tstate->buffer == NULL) {
1287+
PyErr_NoMemory();
1288+
return -1;
1289+
}
1290+
}
12841291
_PyUOpInstruction *buffer = tstate->buffer;
12851292
OPT_STAT_INC(attempts);
12861293
char *env_var = Py_GETENV("PYTHON_UOPS_OPTIMIZE");

Python/pylifecycle.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,11 @@ finalize_modules(PyThreadState *tstate)
17041704
interp->jit = false;
17051705
#ifdef _Py_TIER2
17061706
_Py_Executors_InvalidateAll(interp, 0);
1707+
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
1708+
if (_tstate->buffer != NULL) {
1709+
PyMem_RawFree(_tstate->buffer);
1710+
_tstate->buffer = NULL;
1711+
}
17071712
#endif
17081713

17091714
// Stop watching __builtin__ modifications

0 commit comments

Comments
 (0)