Skip to content

Commit 4ee6bdf

Browse files
authored
gh-115727: Reduce confidence even on 100% predicted jumps (#115748)
The theory is that even if we saw a jump go in the same direction the last 16 times we got there, we shouldn't be overly confident that it's still going to go the same way in the future. This PR makes it so that in the extreme cases, the confidence is multiplied by 0.9 instead of remaining unchanged. For unpredictable jumps, there is no difference (still 0.5). For somewhat predictable jumps, we interpolate.
1 parent 1002fbe commit 4ee6bdf

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Python/optimizer.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ translate_bytecode_to_trace(
546546
uint32_t oparg = instr->op.arg;
547547
uint32_t extended = 0;
548548

549+
DPRINTF(3, "%d: %s(%d)\n", target, _PyOpcode_OpName[opcode], oparg);
550+
549551
if (opcode == ENTER_EXECUTOR) {
550552
assert(oparg < 256);
551553
_PyExecutorObject *executor = code->co_executors->executors[oparg];
@@ -593,21 +595,25 @@ translate_bytecode_to_trace(
593595
int counter = instr[1].cache;
594596
int bitcount = _Py_popcount32(counter);
595597
int jump_likely = bitcount > 8;
598+
/* If bitcount is 8 (half the jumps were taken), adjust confidence by 50%.
599+
If it's 16 or 0 (all or none were taken), adjust by 10%
600+
(since the future is still somewhat uncertain).
601+
For values in between, adjust proportionally. */
596602
if (jump_likely) {
597-
confidence = confidence * bitcount / 16;
603+
confidence = confidence * (bitcount + 2) / 20;
598604
}
599605
else {
600-
confidence = confidence * (16 - bitcount) / 16;
606+
confidence = confidence * (18 - bitcount) / 20;
601607
}
608+
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_likely];
609+
DPRINTF(2, "%d: %s(%d): counter=%x, bitcount=%d, likely=%d, confidence=%d, uopcode=%s\n",
610+
target, _PyOpcode_OpName[opcode], oparg,
611+
counter, bitcount, jump_likely, confidence, _PyUOpName(uopcode));
602612
if (confidence < CONFIDENCE_CUTOFF) {
603-
DPRINTF(2, "Confidence too low (%d)\n", confidence);
613+
DPRINTF(2, "Confidence too low (%d < %d)\n", confidence, CONFIDENCE_CUTOFF);
604614
OPT_STAT_INC(low_confidence);
605615
goto done;
606616
}
607-
uint32_t uopcode = BRANCH_TO_GUARD[opcode - POP_JUMP_IF_FALSE][jump_likely];
608-
DPRINTF(2, "%s(%d): counter=%x, bitcount=%d, likely=%d, confidence=%d, uopcode=%s\n",
609-
_PyOpcode_OpName[opcode], oparg,
610-
counter, bitcount, jump_likely, confidence, _PyUOpName(uopcode));
611617
_Py_CODEUNIT *next_instr = instr + 1 + _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
612618
_Py_CODEUNIT *target_instr = next_instr + oparg;
613619
if (jump_likely) {

0 commit comments

Comments
 (0)