Skip to content

Commit 3618240

Browse files
authored
pythongh-126835: Avoid creating unnecessary tuple when looking for constant sequence during constant folding (python#131054)
1 parent b528669 commit 3618240

File tree

3 files changed

+141
-94
lines changed

3 files changed

+141
-94
lines changed

Diff for: Include/internal/pycore_compile.h

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ extern "C" {
1414
#include "pycore_symtable.h" // _Py_SourceLocation
1515
#include "pycore_instruction_sequence.h"
1616

17+
/* A soft limit for stack use, to avoid excessive
18+
* memory use for large constants, etc.
19+
*
20+
* The value 30 is plucked out of thin air.
21+
* Code that could use more stack than this is
22+
* rare, so the exact value is unimportant.
23+
*/
24+
#define _PY_STACK_USE_GUIDELINE 30
25+
1726
struct _arena; // Type defined in pycore_pyarena.h
1827
struct _mod; // Type defined in pycore_ast.h
1928

Diff for: Python/codegen.c

+7-16
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@
3737
#define COMP_SETCOMP 2
3838
#define COMP_DICTCOMP 3
3939

40-
/* A soft limit for stack use, to avoid excessive
41-
* memory use for large constants, etc.
42-
*
43-
* The value 30 is plucked out of thin air.
44-
* Code that could use more stack than this is
45-
* rare, so the exact value is unimportant.
46-
*/
47-
#define STACK_USE_GUIDELINE 30
48-
4940
#undef SUCCESS
5041
#undef ERROR
5142
#define SUCCESS 0
@@ -3209,7 +3200,7 @@ starunpack_helper_impl(compiler *c, location loc,
32093200
int build, int add, int extend, int tuple)
32103201
{
32113202
Py_ssize_t n = asdl_seq_LEN(elts);
3212-
int big = n + pushed + (injected_arg ? 1 : 0) > STACK_USE_GUIDELINE;
3203+
int big = n + pushed + (injected_arg ? 1 : 0) > _PY_STACK_USE_GUIDELINE;
32133204
int seen_star = 0;
32143205
for (Py_ssize_t i = 0; i < n; i++) {
32153206
expr_ty elt = asdl_seq_GET(elts, i);
@@ -3364,7 +3355,7 @@ static int
33643355
codegen_subdict(compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
33653356
{
33663357
Py_ssize_t i, n = end - begin;
3367-
int big = n*2 > STACK_USE_GUIDELINE;
3358+
int big = n*2 > _PY_STACK_USE_GUIDELINE;
33683359
location loc = LOC(e);
33693360
if (big) {
33703361
ADDOP_I(c, loc, BUILD_MAP, 0);
@@ -3411,7 +3402,7 @@ codegen_dict(compiler *c, expr_ty e)
34113402
ADDOP_I(c, loc, DICT_UPDATE, 1);
34123403
}
34133404
else {
3414-
if (elements*2 > STACK_USE_GUIDELINE) {
3405+
if (elements*2 > _PY_STACK_USE_GUIDELINE) {
34153406
RETURN_IF_ERROR(codegen_subdict(c, e, i - elements, i + 1));
34163407
if (have_dict) {
34173408
ADDOP_I(c, loc, DICT_UPDATE, 1);
@@ -3752,7 +3743,7 @@ maybe_optimize_method_call(compiler *c, expr_ty e)
37523743
/* Check that there aren't too many arguments */
37533744
argsl = asdl_seq_LEN(args);
37543745
kwdsl = asdl_seq_LEN(kwds);
3755-
if (argsl + kwdsl + (kwdsl != 0) >= STACK_USE_GUIDELINE) {
3746+
if (argsl + kwdsl + (kwdsl != 0) >= _PY_STACK_USE_GUIDELINE) {
37563747
return 0;
37573748
}
37583749
/* Check that there are no *varargs types of arguments. */
@@ -3849,7 +3840,7 @@ codegen_joined_str(compiler *c, expr_ty e)
38493840
{
38503841
location loc = LOC(e);
38513842
Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
3852-
if (value_count > STACK_USE_GUIDELINE) {
3843+
if (value_count > _PY_STACK_USE_GUIDELINE) {
38533844
_Py_DECLARE_STR(empty, "");
38543845
ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty)));
38553846
ADDOP_NAME(c, loc, LOAD_METHOD, &_Py_ID(join), names);
@@ -3928,7 +3919,7 @@ codegen_subkwargs(compiler *c, location loc,
39283919
Py_ssize_t i, n = end - begin;
39293920
keyword_ty kw;
39303921
assert(n > 0);
3931-
int big = n*2 > STACK_USE_GUIDELINE;
3922+
int big = n*2 > _PY_STACK_USE_GUIDELINE;
39323923
if (big) {
39333924
ADDOP_I(c, NO_LOCATION, BUILD_MAP, 0);
39343925
}
@@ -3981,7 +3972,7 @@ codegen_call_helper_impl(compiler *c, location loc,
39813972
nelts = asdl_seq_LEN(args);
39823973
nkwelts = asdl_seq_LEN(keywords);
39833974

3984-
if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
3975+
if (nelts + nkwelts*2 > _PY_STACK_USE_GUIDELINE) {
39853976
goto ex_call;
39863977
}
39873978
for (i = 0; i < nelts; i++) {

0 commit comments

Comments
 (0)