@@ -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
5354static inline _Py_BackoffCounter
5455forge_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
7071static inline _Py_BackoffCounter
@@ -112,7 +113,7 @@ trigger_backoff_counter(void)
112113static inline _Py_BackoffCounter
113114initial_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)
126127static inline _Py_BackoffCounter
127128initial_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. */
134135static inline _Py_BackoffCounter
135136initial_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
0 commit comments