gh-126835: Avoid creating unnecessary tuple when looking for constant sequence during constant folding (#131054)

This commit is contained in:
Yan Yanchii 2025-03-12 22:45:54 +01:00 committed by GitHub
parent b52866953a
commit 3618240624
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 143 additions and 96 deletions

View file

@ -37,15 +37,6 @@
#define COMP_SETCOMP 2
#define COMP_DICTCOMP 3
/* A soft limit for stack use, to avoid excessive
* memory use for large constants, etc.
*
* The value 30 is plucked out of thin air.
* Code that could use more stack than this is
* rare, so the exact value is unimportant.
*/
#define STACK_USE_GUIDELINE 30
#undef SUCCESS
#undef ERROR
#define SUCCESS 0
@ -3209,7 +3200,7 @@ starunpack_helper_impl(compiler *c, location loc,
int build, int add, int extend, int tuple)
{
Py_ssize_t n = asdl_seq_LEN(elts);
int big = n + pushed + (injected_arg ? 1 : 0) > STACK_USE_GUIDELINE;
int big = n + pushed + (injected_arg ? 1 : 0) > _PY_STACK_USE_GUIDELINE;
int seen_star = 0;
for (Py_ssize_t i = 0; i < n; i++) {
expr_ty elt = asdl_seq_GET(elts, i);
@ -3364,7 +3355,7 @@ static int
codegen_subdict(compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
{
Py_ssize_t i, n = end - begin;
int big = n*2 > STACK_USE_GUIDELINE;
int big = n*2 > _PY_STACK_USE_GUIDELINE;
location loc = LOC(e);
if (big) {
ADDOP_I(c, loc, BUILD_MAP, 0);
@ -3411,7 +3402,7 @@ codegen_dict(compiler *c, expr_ty e)
ADDOP_I(c, loc, DICT_UPDATE, 1);
}
else {
if (elements*2 > STACK_USE_GUIDELINE) {
if (elements*2 > _PY_STACK_USE_GUIDELINE) {
RETURN_IF_ERROR(codegen_subdict(c, e, i - elements, i + 1));
if (have_dict) {
ADDOP_I(c, loc, DICT_UPDATE, 1);
@ -3752,7 +3743,7 @@ maybe_optimize_method_call(compiler *c, expr_ty e)
/* Check that there aren't too many arguments */
argsl = asdl_seq_LEN(args);
kwdsl = asdl_seq_LEN(kwds);
if (argsl + kwdsl + (kwdsl != 0) >= STACK_USE_GUIDELINE) {
if (argsl + kwdsl + (kwdsl != 0) >= _PY_STACK_USE_GUIDELINE) {
return 0;
}
/* Check that there are no *varargs types of arguments. */
@ -3849,7 +3840,7 @@ codegen_joined_str(compiler *c, expr_ty e)
{
location loc = LOC(e);
Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
if (value_count > STACK_USE_GUIDELINE) {
if (value_count > _PY_STACK_USE_GUIDELINE) {
_Py_DECLARE_STR(empty, "");
ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty)));
ADDOP_NAME(c, loc, LOAD_METHOD, &_Py_ID(join), names);
@ -3928,7 +3919,7 @@ codegen_subkwargs(compiler *c, location loc,
Py_ssize_t i, n = end - begin;
keyword_ty kw;
assert(n > 0);
int big = n*2 > STACK_USE_GUIDELINE;
int big = n*2 > _PY_STACK_USE_GUIDELINE;
if (big) {
ADDOP_I(c, NO_LOCATION, BUILD_MAP, 0);
}
@ -3981,7 +3972,7 @@ codegen_call_helper_impl(compiler *c, location loc,
nelts = asdl_seq_LEN(args);
nkwelts = asdl_seq_LEN(keywords);
if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
if (nelts + nkwelts*2 > _PY_STACK_USE_GUIDELINE) {
goto ex_call;
}
for (i = 0; i < nelts; i++) {