Index: Python/peephole.c =================================================================== --- Python/peephole.c (revision 61328) +++ Python/peephole.c (working copy) @@ -19,6 +19,8 @@ #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) #define ISBASICBLOCK(blocks, start, bytes) \ (blocks[start]==blocks[start+bytes-1]) +#define ISBASICBLOCKSTART(blocks, i) \ + (i==0 || blocks[i-1] < blocks[i]) /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n with LOAD_CONST (c1, c2, ... cn). @@ -516,6 +518,24 @@ } break; } + + /* Optimize a common case: jump to a POP_TOP JUMP_(FORWARD|ABSOLUTE) + where the second jump leads to a command directly following + another POP_TOP. In this case, we can make the first conditional + jump target the second POP_TOP right away. */ + tgt = GETJUMPTGT(codestr, i); + if (codestr[tgt] == POP_TOP && + UNCONDITIONAL_JUMP(codestr[tgt+1])) { + tgttgt = GETJUMPTGT(codestr, tgt+1); + while (UNCONDITIONAL_JUMP(codestr[tgttgt])) + tgttgt = GETJUMPTGT(codestr, tgttgt); + if (ISBASICBLOCKSTART(blocks, tgttgt-1) && + codestr[tgttgt-1] == POP_TOP) { + j = (tgttgt-1) - (i+3); + SETARG(codestr, i, j); + /* memset(codestr + tgt, NOP, 1 + CODESIZE(codestr[tgt+1])); */ + } + } /* Intentional fallthrough */ /* Replace jumps to unconditional jumps */