@@ -812,8 +812,10 @@ _PyJit_translate_single_bytecode_to_trace(
812812 goto done ;
813813 }
814814
815- // Snapshot the buffer position before emitting uops for this bytecode.
815+ // Snapshot the buffer before reserving tail slots. The later charge
816+ // includes both emitted uops and capacity reserved for exits/deopts/errors.
816817 _PyUOpInstruction * next_before = trace -> next ;
818+ _PyUOpInstruction * end_before = trace -> end ;
817819
818820 // One for possible _DEOPT, one because _CHECK_VALIDITY itself might _DEOPT
819821 trace -> end -= 2 ;
@@ -1056,12 +1058,16 @@ _PyJit_translate_single_bytecode_to_trace(
10561058 ADD_TO_TRACE (_JUMP_TO_TOP , 0 , 0 , 0 );
10571059 goto done ;
10581060 }
1059- // Charge fitness by the number of uops actually emitted for this bytecode.
1061+ // Charge fitness by trace-buffer capacity consumed for this bytecode,
1062+ // including both emitted uops and tail reservations.
10601063 {
1061- int32_t slots_used = (int32_t )(trace -> next - next_before );
1064+ int32_t slots_fwd = (int32_t )(trace -> next - next_before );
1065+ int32_t slots_rev = (int32_t )(end_before - trace -> end );
1066+ int32_t slots_used = slots_fwd + slots_rev ;
10621067 tracer -> translator_state .fitness -= slots_used ;
1063- DPRINTF (3 , " per-insn cost: -%d -> fitness=%d\n" ,
1064- slots_used , tracer -> translator_state .fitness );
1068+ DPRINTF (3 , " per-insn cost: -%d (fwd=%d, rev=%d) -> fitness=%d\n" ,
1069+ slots_used , slots_fwd , slots_rev ,
1070+ tracer -> translator_state .fitness );
10651071 }
10661072 DPRINTF (2 , "Trace continuing (fitness=%d)\n" , tracer -> translator_state .fitness );
10671073 return 1 ;
0 commit comments