8000 Make _Py_BackoffCounter a member of _Py_CODEUNIT · python/cpython@3fee35f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fee35f

Browse files
committed
Make _Py_BackoffCounter a member of _Py_CODEUNIT
This changes a lot of things but the end result is arguably better.
1 parent a7c9b6d commit 3fee35f

File tree

12 files changed

+106
-108
lines changed

12 files changed

+106
-108
lines changed

Include/cpython/code.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ typedef struct _Py_GlobalMonitors {
2424
uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
2525
} _Py_GlobalMonitors;
2626

27+
typedef struct {
28+
union {
29+
struct {
30+
uint16_t backoff : 4;
31+
uint16_t value : 12;
32+
};
33+
uint16_t as_counter; // For printf("%#x", ...)
34+
};
35+
} _Py_BackoffCounter;
36+
2737
/* Each instruction in a code object is a fixed-width value,
2838
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
2939
* opcode allows for larger values but the current limit is 3 uses
@@ -39,6 +49,7 @@ typedef union {
3949
uint8_t code;
4050
uint8_t arg;
4151
} op;
52+
_Py_BackoffCounter counter; // First cache entry of specializable op
4253
} _Py_CODEUNIT;
4354

4455

Include/cpython/optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static inline uint16_t uop_get_error_target(const _PyUOpInstruction *inst)
8989

9090
typedef struct _exit_data {
9191
uint32_t target;
92-
uint16_t temperature;
92+
_Py_BackoffCounter temperature;
9393
const struct _PyExecutorObject *executor;
9494
} _PyExitData;
9595

Include/internal/pycore_backoff.h

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,30 @@ extern "C" {
3131
There is an exceptional value which must not be updated, 0xFFFF.
3232
*/
3333

34-
typedef struct {
35-
union {
36-
uint16_t counter;
37-
struct {
38-
uint16_t backoff : 4;
39-
uint16_t value : 12;
40-
};
41-
};
42-
} backoff_counter_t;
43-
44-
static_assert(sizeof(backoff_counter_t) == sizeof(_Py_CODEUNIT),
45-
"backoff counter size should be the same size as a code unit");
46-
4734
#define UNREACHABLE_BACKOFF 0xFFFF
4835

4936
static inline bool
50-
is_unreachable_backoff_counter(backoff_counter_t counter)
37+
is_unreachable_backoff_counter(_Py_BackoffCounter counter)
5138
{
52-
return counter.counter == UNREACHABLE_BACKOFF;
39+
return counter.as_counter == UNREACHABLE_BACKOFF;
5340
}
5441

55-
static inline backoff_counter_t
42+
static inline _Py_BackoffCounter
5643
make_backoff_counter(uint16_t value, uint16_t backoff)
5744
{
5845
assert(backoff <= 15);
5946
assert(value <= 0xFFF);
60-
return (backoff_counter_t){.value = value, .backoff = backoff};
47+
return (_Py_BackoffCounter){.value = value, .backoff = backoff};
6148
}
6249

63-
static inline backoff_counter_t
50+
static inline _Py_BackoffCounter
6451
forge_backoff_counter(uint16_t counter)
6552
{
66-
return (backoff_counter_t){.counter = counter};
53+
return (_Py_BackoffCounter){.as_counter = counter};
6754
}
6855

69-
static inline backoff_counter_t
70-
restart_backoff_counter(backoff_counter_t counter)
56+
static inline _Py_BackoffCounter
57+
restart_backoff_counter(_Py_BackoffCounter counter)
7158
{
7259
assert(!is_unreachable_backoff_counter(counter));
7360
if (counter.backoff < 12) {
@@ -78,14 +65,14 @@ restart_backoff_counter(backoff_counter_t counter)
7865
}
7966
}
8067

81-
static inline backoff_counter_t
82-
pause_backoff_counter(backoff_counter_t counter)
68+
static inline _Py_BackoffCounter
69+
pause_backoff_counter(_Py_BackoffCounter counter)
8370
{
8471
return make_backoff_counter(counter.value | 1, counter.backoff);
8572
}
8673

87-
static inline backoff_counter_t
88-
advance_backoff_counter(backoff_counter_t counter)
74+
static inline _Py_BackoffCounter
75+
advance_backoff_counter(_Py_BackoffCounter counter)
8976
{
9077
if (!is_unreachable_backoff_counter(counter)) {
9178
return make_backoff_counter((counter.value - 1) & 0xFFF, counter.backoff);
@@ -96,16 +83,16 @@ advance_backoff_counter(backoff_counter_t counter)
9683
}
9784

9885
static inline bool
99-
backoff_counter_triggers(backoff_counter_t counter)
86+
backoff_counter_triggers(_Py_BackoffCounter counter)
10087
{
10188
return counter.value == 0;
10289
}
10390

104-
static inline uint16_t
91+
static inline _Py_BackoffCounter
10592
initial_backoff_counter(void)
10693
{
10794
// Backoff sequence 16, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096
108-
return make_backoff_counter(16, 3).counter;
95+
return make_backoff_counter(16, 3);
10996
}
11097

11198
#ifdef __cplusplus

Include/internal/pycore_code.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C" {
3131
#define CACHE_ENTRIES(cache) (sizeof(cache)/sizeof(_Py_CODEUNIT))
3232

3333
typedef struct {
34-
uint16_t counter;
34+
_Py_BackoffCounter counter;
3535
uint16_t module_keys_version;
3636
uint16_t builtin_keys_version;
3737
uint16_t index;
@@ -40,44 +40,44 @@ typedef struct {
4040
#define INLINE_CACHE_ENTRIES_LOAD_GLOBAL CACHE_ENTRIES(_PyLoadGlobalCache)
4141

4242
typedef struct {
43-
uint16_t counter;
43+
_Py_BackoffCounter counter;
4444
} _PyBinaryOpCache;
4545

4646
#define INLINE_CACHE_ENTRIES_BINARY_OP CACHE_ENTRIES(_PyBinaryOpCache)
4747

4848
typedef struct {
49-
uint16_t counter;
49+
_Py_BackoffCounter counter;
5050
} _PyUnpackSequenceCache;
5151

5252
#define INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE \
5353
CACHE_ENTRIES(_PyUnpackSequenceCache)
5454

5555
typedef struct {
56-
uint16_t counter;
56+
_Py_BackoffCounter counter;
5757
} _PyCompareOpCache;
5858

5959
#define INLINE_CACHE_ENTRIES_COMPARE_OP CACHE_ENTRIES(_PyCompareOpCache)
6060

6161
typedef struct {
62-
uint16_t counter;
62+
_Py_BackoffCounter counter;
6363
} _PyBinarySubscrCache;
6464

6565
#define INLINE_CACHE_ENTRIES_BINARY_SUBSCR CACHE_ENTRIES(_PyBinarySubscrCache)
6666

6767
typedef struct {
68-
uint16_t counter;
68+
_Py_BackoffCounter counter;
6969
} _PySuperAttrCache;
7070

7171
#define INLINE_CACHE_ENTRIES_LOAD_SUPER_ATTR CACHE_ENTRIES(_PySuperAttrCache)
7272

7373
typedef struct {
74-
uint16_t counter;
74+
_Py_BackoffCounter counter;
7575
uint16_t version[2];
7676
uint16_t index;
7777
} _PyAttrCache;
7878

7979
typedef struct {
80-
uint16_t counter;
80+
_Py_BackoffCounter counter;
8181
uint16_t type_version[2];
8282
union {
8383
uint16_t keys_version[2];
@@ -93,39 +93,39 @@ typedef struct {
9393
#define INLINE_CACHE_ENTRIES_STORE_ATTR CACHE_ENTRIES(_PyAttrCache)
9494

9595
typedef struct {
96-
uint16_t counter;
96+
_Py_BackoffCounter counter;
9797
uint16_t func_version[2];
9898
} _PyCallCache;
9999

100100
#define INLINE_CACHE_ENTRIES_CALL CACHE_ENTRIES(_PyCallCache)
101101

102102
typedef struct {
103-
uint16_t counter;
103+
_Py_BackoffCounter counter;
104104
} _PyStoreSubscrCache;
105105

106106
#define INLINE_CACHE_ENTRIES_STORE_SUBSCR CACHE_ENTRIES(_PyStoreSubscrCache)
107107

108108
typedef struct {
109-
uint16_t counter;
109+
_Py_BackoffCounter counter;
110110
} _PyForIterCache;
111111

112112
#define INLINE_CACHE_ENTRIES_FOR_ITER CACHE_ENTRIES(_PyForIterCache)
113113

114114
typedef struct {
115-
uint16_t counter;
115+
_Py_BackoffCounter counter;
116116
} _PySendCache;
117117

118118
#define INLINE_CACHE_ENTRIES_SEND CACHE_ENTRIES(_PySendCache)
119119

120120
typedef struct {
121-
uint16_t counter;
121+
_Py_BackoffCounter counter;
122122
uint16_t version[2];
123123
} _PyToBoolCache;
124124

125125
#define INLINE_CACHE_ENTRIES_TO_BOOL CACHE_ENTRIES(_PyToBoolCache)
126126

127127
typedef struct {
128-
uint16_t counter;
128+
_Py_BackoffCounter counter;
129129
} _PyContainsOpCache;
130130

131131
#define INLINE_CACHE_ENTRIES_CONTAINS_OP CACHE_ENTRIES(_PyContainsOpCache)
@@ -476,26 +476,26 @@ write_location_entry_start(uint8_t *ptr, int code, int length)
476476
#define ADAPTIVE_COOLDOWN_VALUE 52
477477
#define ADAPTIVE_COOLDOWN_BACKOFF 0
478478

479-
static inline uint16_t
479+
static inline _Py_BackoffCounter
480480
adaptive_counter_bits(uint16_t value, uint16_t backoff) {
481-
return make_backoff_counter(value, backoff).counter;
481+
return make_backoff_counter(value, backoff);
482482
}
483483

484-
static inline uint16_t
484+
static inline _Py_BackoffCounter
485485
adaptive_counter_warmup(void) {
486486
return adaptive_counter_bits(ADAPTIVE_WARMUP_VALUE,
487487
ADAPTIVE_WARMUP_BACKOFF);
488488
}
489489

490-
static inline uint16_t
490+
static inline _Py_BackoffCounter
491491
adaptive_counter_cooldown(void) {
492492
return adaptive_counter_bits(ADAPTIVE_COOLDOWN_VALUE,
493493
ADAPTIVE_COOLDOWN_BACKOFF);
494494
}
495495

496-
static inline uint16_t
497-
adaptive_counter_backoff(uint16_t counter) {
498-
return restart_backoff_counter(forge_backoff_counter(counter)).counter;
496+
static inline _Py_BackoffCounter
497+
adaptive_counter_backoff(_Py_BackoffCounter counter) {
498+
return restart_backoff_counter(counter);
499499
}
500500

501501

0 commit comments

Comments
 (0)
0