Skip to content

Commit 0e05712

Browse files
Use MAKE_BACKOFF_COUNTER macro
1 parent 6d44333 commit 0e05712

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

Include/internal/pycore_backoff.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ extern "C" {
3535
#define BACKOFF_MASK 7
3636
#define MAX_BACKOFF 6
3737
#define UNREACHABLE_BACKOFF 7
38-
#define MAX_VALUE 0x1FFF
3938

40-
// We only use values x such that x + 1 is prime.
41-
static const int lookup_table[] = {1, 6, 30, 126, 508, 2052, 8190, 8190};
42-
43-
static inline _Py_BackoffCounter
44-
make_backoff_counter(uint16_t value, uint16_t backoff)
45-
{
46-
assert(backoff <= UNREACHABLE_BACKOFF);
47-
assert(value <= MAX_VALUE);
48-
_Py_BackoffCounter result;
49-
result.value_and_backoff = (value << BACKOFF_BITS) | backoff;
50-
return result;
51-
}
39+
#define MAKE_BACKOFF_COUNTER(value, backoff) \
40+
((_Py_BackoffCounter){ .value_and_backoff = (value << BACKOFF_BITS) | backoff })
41+
42+
// We only use values x and backoffs b such that
43+
// x + 1 is prime and is near to 2**(2*b+1).
44+
static const _Py_BackoffCounter backoff_counter_table[] = {
45+
MAKE_BACKOFF_COUNTER(1, 0),
46+
MAKE_BACKOFF_COUNTER(6, 1),
47+
MAKE_BACKOFF_COUNTER(30, 2),
48+
MAKE_BACKOFF_COUNTER(126, 3),
49+
MAKE_BACKOFF_COUNTER(508, 4),
50+
MAKE_BACKOFF_COUNTER(2052, 5),
51+
MAKE_BACKOFF_COUNTER(8190, 6),
52+
};
5253

5354
static inline _Py_BackoffCounter
5455
forge_backoff_counter(uint16_t counter)
@@ -63,8 +64,8 @@ restart_backoff_counter(_Py_BackoffCounter counter)
6364
{
6465
uint16_t backoff = counter.value_and_backoff & BACKOFF_MASK;
6566
assert(backoff <= MAX_BACKOFF);
66-
backoff = (backoff == MAX_BACKOFF) ? backoff : backoff + 1;
67-
return make_backoff_counter(lookup_table[backoff], backoff);
67+
backoff = (backoff >= MAX_BACKOFF) ? MAX_BACKOFF : backoff + 1;
68+
return backoff_counter_table[backoff];
6869
}
6970

7071
static inline _Py_BackoffCounter
@@ -112,7 +113,7 @@ trigger_backoff_counter(void)
112113
static inline _Py_BackoffCounter
113114
initial_jump_backoff_counter(void)
114115
{
115-
return make_backoff_counter(JUMP_BACKWARD_INITIAL_VALUE,
116+
return MAKE_BACKOFF_COUNTER(JUMP_BACKWARD_INITIAL_VALUE,
116117
JUMP_BACKWARD_INITIAL_BACKOFF);
117118
}
118119

@@ -126,15 +127,15 @@ initial_jump_backoff_counter(void)
126127
static inline _Py_BackoffCounter
127128
initial_temperature_backoff_counter(void)
128129
{
129-
return make_backoff_counter(SIDE_EXIT_INITIAL_VALUE,
130+
return MAKE_BACKOFF_COUNTER(SIDE_EXIT_INITIAL_VALUE,
130131
SIDE_EXIT_INITIAL_BACKOFF);
131132
}
132133

133134
/* Unreachable backoff counter. */
134135
static inline _Py_BackoffCounter
135136
initial_unreachable_backoff_counter(void)
136137
{
137-
return make_backoff_counter(0, UNREACHABLE_BACKOFF);
138+
return MAKE_BACKOFF_COUNTER(0, UNREACHABLE_BACKOFF);
138139
}
139140

140141
#ifdef __cplusplus

Include/internal/pycore_code.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,21 +466,16 @@ write_location_entry_start(uint8_t *ptr, int code, int length)
466466
# error "Cold exit value should be larger than adaptive cooldown value"
467467
#endif
468468

469-
static inline _Py_BackoffCounter
470-
adaptive_counter_bits(uint16_t value, uint16_t backoff) {
471-
return make_backoff_counter(value, backoff);
472-
}
473-
474469
static inline _Py_BackoffCounter
475470
adaptive_counter_warmup(void) {
476-
return adaptive_counter_bits(ADAPTIVE_WARMUP_VALUE,
477-
ADAPTIVE_WARMUP_BACKOFF);
471+
return MAKE_BACKOFF_COUNTER(ADAPTIVE_WARMUP_VALUE,
472+
ADAPTIVE_WARMUP_BACKOFF);
478473
}
479474

480475
static inline _Py_BackoffCounter
481476
adaptive_counter_cooldown(void) {
482-
return adaptive_counter_bits(ADAPTIVE_COOLDOWN_VALUE,
483-
ADAPTIVE_COOLDOWN_BACKOFF);
477+
return MAKE_BACKOFF_COUNTER(ADAPTIVE_COOLDOWN_VALUE,
478+
ADAPTIVE_COOLDOWN_BACKOFF);
484479
}
485480

486481
static inline _Py_BackoffCounter

0 commit comments

Comments
 (0)