Skip to content

bpo-46528: Simplify BUILD_TUPLE/UNPACK_SEQUENCE folding #31039

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 1 commit into from
Feb 1, 2022
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
2 changes: 1 addition & 1 deletion Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_pack_unpack(self):
code = compile(line,'','single')
self.assertInBytecode(code, elem)
self.assertNotInBytecode(code, 'BUILD_TUPLE')
self.assertNotInBytecode(code, 'UNPACK_TUPLE')
self.assertNotInBytecode(code, 'UNPACK_SEQUENCE')
self.check_lnotab(code)

def test_folding_of_tuples_of_constants(self):
Expand Down
21 changes: 7 additions & 14 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8650,29 +8650,22 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
}

/* Try to fold tuples of constants.
Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
Skip over BUILD_TUPLE(1) UNPACK_SEQUENCE(1).
Replace BUILD_TUPLE(2) UNPACK_SEQUENCE(2) with SWAP(2).
Replace BUILD_TUPLE(3) UNPACK_SEQUENCE(3) with SWAP(3). */
case BUILD_TUPLE:
if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
switch(oparg) {
case 1:
inst->i_opcode = NOP;
bb->b_instr[i+1].i_opcode = NOP;
break;
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this? Aren't they equivalent.
Having a continue here distracts from the flow of the code, as it is not the normal way to end a case and increases cognitive load (at least it did for me).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They’re not quite equivalent: note that I also removed the break on old line 8675.

Previously, values other than 1, 2, or 3 wouldn’t get a chance to hit fold_tuple_on_constants. Now they do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM then.

case 2:
inst->i_opcode = SWAP;
inst->i_oparg = 2;
bb->b_instr[i+1].i_opcode = NOP;
i--;
break;
case 3:
inst->i_opcode = SWAP;
inst->i_oparg = 3;
bb->b_instr[i+1].i_opcode = NOP;
i--;
inst->i_opcode = NOP;
bb->b_instr[i+1].i_opcode = SWAP;
continue;
}
break;
}
if (i >= oparg) {
if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
Expand Down