Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ extern "C" {
#include "pycore_symtable.h" // _Py_SourceLocation
#include "pycore_instruction_sequence.h"

/* 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 _PY_STACK_USE_GUIDELINE 30

struct _arena; // Type defined in pycore_pyarena.h
struct _mod; // Type defined in pycore_ast.h

Expand Down
23 changes: 7 additions & 16 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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++) {
Expand Down
Loading
Loading