Skip to content

Commit 59e4693

Browse files
authored
gh-108488: Initialize JUMP_BACKWARD cache to 0, not 17 (#108591)
This mis-initialization caused the executor optimization to kick in sooner than intended. It also set the lower 4 bits of the counter to `1` -- those bits are supposed to be reserved (the actual counter is in the upper 12 bits).
1 parent 4f22152 commit 59e4693

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,7 @@ def testfunc(a):
24782478

24792479
opt = _testinternalcapi.get_uop_optimizer()
24802480
with temporary_optimizer(opt):
2481-
testfunc([1, 2, 3])
2481+
testfunc(range(10))
24822482

24832483
ex = get_first_executor(testfunc)
24842484
self.assertIsNotNone(ex)
@@ -2493,7 +2493,7 @@ def testfunc(a):
24932493

24942494
opt = _testinternalcapi.get_uop_optimizer()
24952495
with temporary_optimizer(opt):
2496-
testfunc([1, 2, 3])
2496+
testfunc(range(10))
24972497

24982498
ex = get_first_executor(testfunc)
24992499
self.assertIsNotNone(ex)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change the initialization of inline cache entries so that the cache entry for ``JUMP_BACKWARD`` is initialized to zero, instead of the ``adaptive_counter_warmup()`` value used for all other instructions. This counter, unique among instructions, counts up from zero.

Python/specialize.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ _PyCode_Quicken(PyCodeObject *code)
302302
assert(opcode < MIN_INSTRUMENTED_OPCODE);
303303
int caches = _PyOpcode_Caches[opcode];
304304
if (caches) {
305-
instructions[i + 1].cache = adaptive_counter_warmup();
305+
// JUMP_BACKWARD counter counts up from 0 until it is > backedge_threshold
306+
instructions[i + 1].cache =
307+
opcode == JUMP_BACKWARD ? 0 : adaptive_counter_warmup();
306308
i += caches;
307309
}
308310
}

0 commit comments

Comments
 (0)