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

Skip to content

Commit 255a3d0

Browse files
committed
Extend SF patch #707257: Improve code generation
to cover the case for: "x,y,z=1,2,3". Gives a 30% speed-up. Also, added FOR_ITER to the list of opcodes that can jump.
1 parent 8b7a9a3 commit 255a3d0

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

Python/compile.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,23 +364,35 @@ optimize_code(PyObject *code, PyObject* consts)
364364
break;
365365

366366
/* Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2 JMP+2.
367+
Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2 JMP+1.
367368
Note, these opcodes occur together only in assignment
368369
statements. Accordingly, the unpack opcode is never
369370
a jump target. */
370371
case BUILD_TUPLE:
371372
case BUILD_LIST:
372-
if (codestr[i+3] != UNPACK_SEQUENCE ||
373-
GETARG(codestr, i) != 2 ||
374-
GETARG(codestr, i+3) != 2)
373+
if (codestr[i+3] != UNPACK_SEQUENCE)
375374
continue;
376-
codestr[i] = ROT_TWO;
377-
codestr[i+1] = JUMP_FORWARD;
378-
SETARG(codestr, i+1, 2);
379-
codestr[i+4] = DUP_TOP; /* Filler codes used as NOPs */
380-
codestr[i+5] = POP_TOP;
375+
if (GETARG(codestr, i) == 2 && \
376+
GETARG(codestr, i+3) == 2) {
377+
codestr[i] = ROT_TWO;
378+
codestr[i+1] = JUMP_FORWARD;
379+
SETARG(codestr, i+1, 2);
380+
codestr[i+4] = DUP_TOP; /* Filler codes used as NOPs */
381+
codestr[i+5] = POP_TOP;
382+
continue;
383+
}
384+
if (GETARG(codestr, i) == 3 && \
385+
GETARG(codestr, i+3) == 3) {
386+
codestr[i] = ROT_THREE;
387+
codestr[i+1] = ROT_TWO;
388+
codestr[i+2] = JUMP_FORWARD;
389+
SETARG(codestr, i+2, 1);
390+
codestr[i+5] = DUP_TOP;
391+
}
381392
break;
382393

383394
/* Replace jumps to unconditional jumps */
395+
case FOR_ITER:
384396
case JUMP_FORWARD:
385397
case JUMP_IF_FALSE:
386398
case JUMP_IF_TRUE:

0 commit comments

Comments
 (0)