Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e441cb7

Browse files
committed
Issue #1754094: Improve the stack depth calculation in the compiler.
There should be no other effect than a small decrease in memory use.
1 parent 46afef3 commit e441cb7

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #1754094: Improve the stack depth calculation in the compiler.
16+
There should be no other effect than a small decrease in memory use.
17+
Patch by Christopher Tur Lesniewski-Laas.
18+
1519
- Issue #7065: Fix a crash in bytes.maketrans and bytearray.maketrans when
1620
using byte values greater than 127. Patch by egreen.
1721

Python/compile.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ opcode_stack_effect(int opcode, int oparg)
772772
case UNPACK_EX:
773773
return (oparg&0xFF) + (oparg>>8);
774774
case FOR_ITER:
775-
return 1;
775+
return 1; /* or -1, at end of iterator */
776776

777777
case STORE_ATTR:
778778
return -2;
@@ -799,7 +799,7 @@ opcode_stack_effect(int opcode, int oparg)
799799
case COMPARE_OP:
800800
return -1;
801801
case IMPORT_NAME:
802-
return 0;
802+
return -1;
803803
case IMPORT_FROM:
804804
return 1;
805805

@@ -3546,7 +3546,7 @@ dfs(struct compiler *c, basicblock *b, struct assembler *a)
35463546
static int
35473547
stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
35483548
{
3549-
int i;
3549+
int i, target_depth;
35503550
struct instr *instr;
35513551
if (b->b_seen || b->b_startdepth >= depth)
35523552
return maxdepth;
@@ -3559,8 +3559,17 @@ stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
35593559
maxdepth = depth;
35603560
assert(depth >= 0); /* invalid code or bug in stackdepth() */
35613561
if (instr->i_jrel || instr->i_jabs) {
3562+
target_depth = depth;
3563+
if (instr->i_opcode == FOR_ITER) {
3564+
target_depth = depth-2;
3565+
} else if (instr->i_opcode == SETUP_FINALLY ||
3566+
instr->i_opcode == SETUP_EXCEPT) {
3567+
target_depth = depth+3;
3568+
if (target_depth > maxdepth)
3569+
maxdepth = target_depth;
3570+
}
35623571
maxdepth = stackdepth_walk(c, instr->i_target,
3563-
depth, maxdepth);
3572+
target_depth, maxdepth);
35643573
if (instr->i_opcode == JUMP_ABSOLUTE ||
35653574
instr->i_opcode == JUMP_FORWARD) {
35663575
goto out; /* remaining code is dead */

0 commit comments

Comments
 (0)