summaryrefslogtreecommitdiffstats
path: root/Python/ast_opt.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2021-11-16 11:01:57 (GMT)
committerGitHub <noreply@github.com>2021-11-16 11:01:57 (GMT)
commitb9310773756f40f77e075f221a90dd41e6964efc (patch)
tree43674ba9f42f2ff81dfd3025956b0ce8e12251f9 /Python/ast_opt.c
parent9bf2cbc4c498812e14f20d86acb61c53928a5a57 (diff)
downloadcpython-b9310773756f40f77e075f221a90dd41e6964efc.zip
cpython-b9310773756f40f77e075f221a90dd41e6964efc.tar.gz
cpython-b9310773756f40f77e075f221a90dd41e6964efc.tar.bz2
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 'Python/ast_opt.c')
-rw-r--r--Python/ast_opt.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c
index f6506ce..356f60e 100644
--- a/Python/ast_opt.c
+++ b/Python/ast_opt.c
@@ -1098,8 +1098,9 @@ _PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
return 0;
}
/* Be careful here to prevent overflow. */
- starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
- tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
+ int recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
+ starting_recursion_depth = (recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
+ recursion_depth * COMPILER_STACK_FRAME_SCALE : recursion_depth;
state->recursion_depth = starting_recursion_depth;
state->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;