bpo-45753: Make recursion checks more efficient. (GH-29524)

* Uses recursion remaining, instead of recursion depth to speed up check against recursion limit.
This commit is contained in:
Mark Shannon 2021-11-16 11:01:57 +00:00 committed by GitHub
parent 9bf2cbc4c4
commit b931077375
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 43 deletions

View file

@ -75,12 +75,12 @@ extern void _PyEval_DeactivateOpCache(void);
/* With USE_STACKCHECK macro defined, trigger stack checks in
_Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit
|| ++tstate->stackcheck_counter > 64);
return (tstate->recursion_remaining-- <= 0
|| (tstate->recursion_remaining & 63) == 0);
}
#else
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit);
return tstate->recursion_remaining-- <= 0;
}
#endif
@ -101,7 +101,7 @@ static inline int _Py_EnterRecursiveCall_inline(const char *where) {
#define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where)
static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) {
tstate->recursion_depth--;
tstate->recursion_remaining++;
}
static inline void _Py_LeaveRecursiveCall_inline(void) {