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

Skip to content

Commit ef0a82b

Browse files
committed
Simplify chains of conditional jumps.
(Suggested by Neal Norwitz.)
1 parent 08b07de commit ef0a82b

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

Python/compile.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,34 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
550550
}
551551
break;
552552

553+
/* Simplify conditional jump to conditional jump where the
554+
result of the first test implies the success of a similar
555+
test or the failure of the opposite test.
556+
Arises in code like:
557+
"a and b or c"
558+
"a and b and c"
559+
x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
560+
x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE y+3
561+
*/
562+
case JUMP_IF_FALSE:
563+
case JUMP_IF_TRUE:
564+
tgt = GETJUMPTGT(codestr, i);
565+
j = codestr[tgt];
566+
if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
567+
if (j == opcode) {
568+
tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
569+
SETARG(codestr, i, tgttgt);
570+
} else {
571+
tgt -= i;
572+
SETARG(codestr, i, tgt);
573+
}
574+
break;
575+
}
576+
/* Intentional fallthrough */
577+
553578
/* Replace jumps to unconditional jumps */
554579
case FOR_ITER:
555580
case JUMP_FORWARD:
556-
case JUMP_IF_FALSE:
557-
case JUMP_IF_TRUE:
558581
case JUMP_ABSOLUTE:
559582
case CONTINUE_LOOP:
560583
case SETUP_LOOP:

0 commit comments

Comments
 (0)