Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a0e55a5

Browse files
authoredFeb 1, 2022
bpo-46528: Simplify BUILD_TUPLE/UNPACK_SEQUENCE folding (pythonGH-31039)
1 parent bebaa95 commit a0e55a5

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed
 

‎Lib/test/test_peepholer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_pack_unpack(self):
126126
code = compile(line,'','single')
127127
self.assertInBytecode(code, elem)
128128
self.assertNotInBytecode(code, 'BUILD_TUPLE')
129-
self.assertNotInBytecode(code, 'UNPACK_TUPLE')
129+
self.assertNotInBytecode(code, 'UNPACK_SEQUENCE')
130130
self.check_lnotab(code)
131131

132132
def test_folding_of_tuples_of_constants(self):

‎Python/compile.c

+7-14
Original file line numberDiff line numberDiff line change
@@ -8661,29 +8661,22 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
86618661
}
86628662

86638663
/* Try to fold tuples of constants.
8664-
Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
8665-
Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
8666-
Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
8664+
Skip over BUILD_TUPLE(1) UNPACK_SEQUENCE(1).
8665+
Replace BUILD_TUPLE(2) UNPACK_SEQUENCE(2) with SWAP(2).
8666+
Replace BUILD_TUPLE(3) UNPACK_SEQUENCE(3) with SWAP(3). */
86678667
case BUILD_TUPLE:
86688668
if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
86698669
switch(oparg) {
86708670
case 1:
86718671
inst->i_opcode = NOP;
86728672
bb->b_instr[i+1].i_opcode = NOP;
8673-
break;
8673+
continue;
86748674
case 2:
8675-
inst->i_opcode = SWAP;
8676-
inst->i_oparg = 2;
8677-
bb->b_instr[i+1].i_opcode = NOP;
8678-
i--;
8679-
break;
86808675
case 3:
8681-
inst->i_opcode = SWAP;
8682-
inst->i_oparg = 3;
8683-
bb->b_instr[i+1].i_opcode = NOP;
8684-
i--;
8676+
inst->i_opcode = NOP;
8677+
bb->b_instr[i+1].i_opcode = SWAP;
8678+
continue;
86858679
}
8686-
break;
86878680
}
86888681
if (i >= oparg) {
86898682
if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {

0 commit comments

Comments
 (0)
Please sign in to comment.