Skip to content

Jump-To-Jump elimination could be more aggressive. #93223

Closed
@sweeneyde

Description

@sweeneyde

Example:

def f():
    for i in range(5):
        if i > 3:
            print(i)
  1           0 RESUME                   0

  2           2 LOAD_GLOBAL              1 (NULL + range)
             14 LOAD_CONST               1 (5)
             16 CALL                     1
             26 GET_ITER
        >>   28 FOR_ITER                21 (to 72)
             30 STORE_FAST               0 (i)

  3          32 LOAD_FAST                0 (i)
             34 LOAD_CONST               2 (3)
             36 COMPARE_OP               4 (>)
             42 POP_JUMP_FORWARD_IF_FALSE    13 (to 70)     <--------- could be BACKWARD to 28

  4          44 LOAD_GLOBAL              3 (NULL + print)
             56 LOAD_FAST                0 (i)
             58 CALL                     1
             68 POP_TOP
        >>   70 JUMP_BACKWARD           22 (to 28)

  2     >>   72 LOAD_CONST               0 (None)
             74 RETURN_VALUE

Something like this should suffice:

 static bool
 jump_thread(struct instr *inst, struct instr *target, int opcode)
 {
     assert(!IS_VIRTUAL_OPCODE(opcode) || IS_VIRTUAL_JUMP_OPCODE(opcode));
     assert(is_jump(inst));
     assert(is_jump(target));
     // bpo-45773: If inst->i_target == target->i_target, then nothing actually
     // changes (and we fall into an infinite loop):
-    if (inst->i_lineno == target->i_lineno &&
+    if ((inst->i_lineno == target->i_lineno || target->i_lineno == -1) &&
         inst->i_target != target->i_target)
     {
         inst->i_target = target->i_target;
         inst->i_opcode = opcode;
         return true;
     }
     return false;
 }

cc @markshannon @iritkatriel @brandtbucher

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions