summaryrefslogtreecommitdiff
path: root/Include
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2021-11-16 11:01:57 +0000
committerGitHub <noreply@github.com>2021-11-16 11:01:57 +0000
commitb9310773756f40f77e075f221a90dd41e6964efc (patch)
tree43674ba9f42f2ff81dfd3025956b0ce8e12251f9 /Include
parent9bf2cbc4c498812e14f20d86acb61c53928a5a57 (diff)
downloadcpython-git-b9310773756f40f77e075f221a90dd41e6964efc.tar.gz
bpo-45753: Make recursion checks more efficient. (GH-29524)
* Uses recursion remaining, instead of recursion depth to speed up check against recursion limit.
Diffstat (limited to 'Include')
-rw-r--r--Include/cpython/pystate.h4
-rw-r--r--Include/internal/pycore_ceval.h8
2 files changed, 6 insertions, 6 deletions
diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h
index cf69c72028..9ac0a298ba 100644
--- a/Include/cpython/pystate.h
+++ b/Include/cpython/pystate.h
@@ -79,9 +79,9 @@ struct _ts {
struct _ts *next;
PyInterpreterState *interp;
- int recursion_depth;
+ int recursion_remaining;
+ int recursion_limit;
int recursion_headroom; /* Allow 50 more calls to handle any errors. */
- int stackcheck_counter;
/* 'tracing' keeps track of the execution depth when tracing/profiling.
This is to prevent the actual trace/profile code from being recorded in
diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h
index 53580b99d3..c2251b04be 100644
--- a/Include/internal/pycore_ceval.h
+++ b/Include/internal/pycore_ceval.h
@@ -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) {