bpo-40513: Per-interpreter recursion_limit (GH-19929)

Move recursion_limit member from _PyRuntimeState.ceval to
PyInterpreterState.ceval.

* Py_SetRecursionLimit() now only sets _Py_CheckRecursionLimit
  of ceval.c if the current Python thread is part of the main
  interpreter.
* Inline _Py_MakeEndRecCheck() into _Py_LeaveRecursiveCall().
* Convert _Py_RecursionLimitLowerWaterMark() macro into a static
  inline function.
This commit is contained in:
Victor Stinner 2020-05-05 16:52:52 +02:00 committed by GitHub
parent 627f701235
commit 4e30ed3af0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 22 deletions

View File

@ -65,12 +65,12 @@ PyAPI_DATA(int) _Py_CheckRecursionLimit;
/* With USE_STACKCHECK macro defined, trigger stack checks in /* With USE_STACKCHECK macro defined, trigger stack checks in
_Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */ _Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */
static inline int _Py_MakeRecCheck(PyThreadState *tstate) { static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
return (++tstate->recursion_depth > _Py_CheckRecursionLimit return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit
|| ++tstate->stackcheck_counter > 64); || ++tstate->stackcheck_counter > 64);
} }
#else #else
static inline int _Py_MakeRecCheck(PyThreadState *tstate) { static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
return (++tstate->recursion_depth > _Py_CheckRecursionLimit); return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit);
} }
#endif #endif
@ -90,20 +90,22 @@ static inline int _Py_EnterRecursiveCall_inline(const char *where) {
#define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where) #define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where)
/* Compute the "lower-water mark" for a recursion limit. When /* Compute the "lower-water mark" for a recursion limit. When
* Py_LeaveRecursiveCall() is called with a recursion depth below this mark, * Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
* the overflowed flag is reset to 0. */ * the overflowed flag is reset to 0. */
#define _Py_RecursionLimitLowerWaterMark(limit) \ static inline int _Py_RecursionLimitLowerWaterMark(int limit) {
(((limit) > 200) \ if (limit > 200) {
? ((limit) - 50) \ return (limit - 50);
: (3 * ((limit) >> 2))) }
else {
#define _Py_MakeEndRecCheck(x) \ return (3 * (limit >> 2));
(--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit)) }
}
static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) { static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) {
if (_Py_MakeEndRecCheck(tstate->recursion_depth)) { tstate->recursion_depth--;
int limit = tstate->interp->ceval.recursion_limit;
if (tstate->recursion_depth < _Py_RecursionLimitLowerWaterMark(limit)) {
tstate->overflowed = 0; tstate->overflowed = 0;
} }
} }

View File

@ -33,6 +33,7 @@ struct _pending_calls {
}; };
struct _ceval_state { struct _ceval_state {
int recursion_limit;
/* Records whether tracing is on for any thread. Counts the number /* Records whether tracing is on for any thread. Counts the number
of threads for which tstate->c_tracefunc is non-NULL, so if the of threads for which tstate->c_tracefunc is non-NULL, so if the
value is 0, we know we don't have to check this thread's value is 0, we know we don't have to check this thread's

View File

@ -14,7 +14,6 @@ extern "C" {
/* ceval state */ /* ceval state */
struct _ceval_runtime_state { struct _ceval_runtime_state {
int recursion_limit;
struct _gil_runtime_state gil; struct _gil_runtime_state gil;
}; };

View File

@ -699,7 +699,6 @@ int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
void void
_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval) _PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
{ {
ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
_Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
_gil_initialize(&ceval->gil); _gil_initialize(&ceval->gil);
} }
@ -707,6 +706,8 @@ _PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
int int
_PyEval_InitState(struct _ceval_state *ceval) _PyEval_InitState(struct _ceval_state *ceval)
{ {
ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
struct _pending_calls *pending = &ceval->pending; struct _pending_calls *pending = &ceval->pending;
assert(pending->lock == NULL); assert(pending->lock == NULL);
@ -730,16 +731,18 @@ _PyEval_FiniState(struct _ceval_state *ceval)
int int
Py_GetRecursionLimit(void) Py_GetRecursionLimit(void)
{ {
struct _ceval_runtime_state *ceval = &_PyRuntime.ceval; PyThreadState *tstate = _PyThreadState_GET();
return ceval->recursion_limit; return tstate->interp->ceval.recursion_limit;
} }
void void
Py_SetRecursionLimit(int new_limit) Py_SetRecursionLimit(int new_limit)
{ {
struct _ceval_runtime_state *ceval = &_PyRuntime.ceval; PyThreadState *tstate = _PyThreadState_GET();
ceval->recursion_limit = new_limit; tstate->interp->ceval.recursion_limit = new_limit;
_Py_CheckRecursionLimit = new_limit; if (_Py_IsMainInterpreter(tstate)) {
_Py_CheckRecursionLimit = new_limit;
}
} }
/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() /* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
@ -750,8 +753,7 @@ Py_SetRecursionLimit(int new_limit)
int int
_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where) _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
{ {
_PyRuntimeState *runtime = tstate->interp->runtime; int recursion_limit = tstate->interp->ceval.recursion_limit;
int recursion_limit = runtime->ceval.recursion_limit;
#ifdef USE_STACKCHECK #ifdef USE_STACKCHECK
tstate->stackcheck_counter = 0; tstate->stackcheck_counter = 0;
@ -760,8 +762,10 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
_PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow"); _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
return -1; return -1;
} }
/* Needed for ABI backwards-compatibility (see bpo-31857) */ if (_Py_IsMainInterpreter(tstate)) {
_Py_CheckRecursionLimit = recursion_limit; /* Needed for ABI backwards-compatibility (see bpo-31857) */
_Py_CheckRecursionLimit = recursion_limit;
}
#endif #endif
if (tstate->recursion_critical) if (tstate->recursion_critical)
/* Somebody asked that we don't check for recursion. */ /* Somebody asked that we don't check for recursion. */