Skip to content

Commit 4558af5

Browse files
authored
bpo-46841: Move the cache for LOAD_GLOBAL inline. (GH-31575)
1 parent da7d99a commit 4558af5

File tree

10 files changed

+284
-232
lines changed

10 files changed

+284
-232
lines changed

Include/internal/pycore_code.h

+48-9
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ typedef struct {
2626
uint32_t dk_version;
2727
} _PyAttrCache;
2828

29-
typedef struct {
30-
uint32_t module_keys_version;
31-
uint32_t builtin_keys_version;
32-
} _PyLoadGlobalCache;
33-
3429
typedef struct {
3530
/* Borrowed ref in LOAD_METHOD */
3631
PyObject *obj;
@@ -57,23 +52,35 @@ typedef union {
5752
_PyEntryZero zero;
5853
_PyAdaptiveEntry adaptive;
5954
_PyAttrCache attr;
60-
_PyLoadGlobalCache load_global;
6155
_PyObjectCache obj;
6256
_PyCallCache call;
6357
} SpecializedCacheEntry;
6458

6559
#define INSTRUCTIONS_PER_ENTRY (sizeof(SpecializedCacheEntry)/sizeof(_Py_CODEUNIT))
6660

61+
/* Inline caches */
62+
63+
#define CACHE_ENTRIES(cache) (sizeof(cache)/sizeof(_Py_CODEUNIT))
64+
65+
typedef struct {
66+
_Py_CODEUNIT counter;
67+
_Py_CODEUNIT index;
68+
_Py_CODEUNIT module_keys_version;
69+
_Py_CODEUNIT _m1;
70+
_Py_CODEUNIT builtin_keys_version;
71+
} _PyLoadGlobalCache;
72+
73+
#define INLINE_CACHE_ENTRIES_LOAD_GLOBAL CACHE_ENTRIES(_PyLoadGlobalCache)
74+
6775
typedef struct {
6876
_Py_CODEUNIT counter;
6977
} _PyBinaryOpCache;
7078

79+
#define INLINE_CACHE_ENTRIES_BINARY_OP CACHE_ENTRIES(_PyBinaryOpCache)
7180
typedef struct {
7281
_Py_CODEUNIT counter;
7382
} _PyUnpackSequenceCache;
7483

75-
#define INLINE_CACHE_ENTRIES_BINARY_OP \
76-
(sizeof(_PyBinaryOpCache) / sizeof(_Py_CODEUNIT))
7784

7885
#define INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE \
7986
(sizeof(_PyUnpackSequenceCache) / sizeof(_Py_CODEUNIT))
@@ -307,7 +314,7 @@ cache_backoff(_PyAdaptiveEntry *entry) {
307314

308315
extern int _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
309316
extern int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
310-
extern int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
317+
extern int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name);
311318
extern int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
312319
extern int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
313320
extern int _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, _Py_CODEUNIT *instr);
@@ -388,6 +395,38 @@ extern PyObject* _Py_GetSpecializationStats(void);
388395
#define OBJECT_STAT_INC(name) ((void)0)
389396
#endif
390397

398+
// Cache values are only valid in memory, so use native endianness.
399+
#ifdef WORDS_BIGENDIAN
400+
401+
static inline void
402+
write32(uint16_t *p, uint32_t val)
403+
{
404+
p[0] = val >> 16;
405+
p[1] = (uint16_t)val;
406+
}
407+
408+
static inline uint32_t
409+
read32(uint16_t *p)
410+
{
411+
return (p[0] << 16) | p[1];
412+
}
413+
414+
#else
415+
416+
static inline void
417+
write32(uint16_t *p, uint32_t val)
418+
{
419+
p[0] = (uint16_t)val;
420+
p[1] = val >> 16;
421+
}
422+
423+
static inline uint32_t
424+
read32(uint16_t *p)
425+
{
426+
return p[0] | (p[1] << 16);
427+
}
428+
429+
#endif
391430

392431
#ifdef __cplusplus
393432
}

Include/opcode.h

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/importlib/_bootstrap_external.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ def _write_atomic(path, data, mode=0o666):
387387
# Python 3.11a5 3478 (New CALL opcodes)
388388
# Python 3.11a5 3479 (Add PUSH_NULL opcode)
389389
# Python 3.11a5 3480 (New CALL opcodes, second iteration)
390-
# Python 3.11a5 3481 (Use inline CACHE instructions)
391-
# Python 3.11a5 3482 (Use inline caching for UNPACK_SEQUENCE)
390+
# Python 3.11a5 3481 (Use inline cache for BINARY_OP)
391+
# Python 3.11a5 3482 (Use inline caching for UNPACK_SEQUENCE and LOAD_GLOBAL)
392392

393393
# Python 3.12 will start with magic number 3500
394394

Lib/opcode.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def jabs_op(name, op, entries=0):
135135
jabs_op('JUMP_ABSOLUTE', 113) # ""
136136
jabs_op('POP_JUMP_IF_FALSE', 114) # ""
137137
jabs_op('POP_JUMP_IF_TRUE', 115) # ""
138-
name_op('LOAD_GLOBAL', 116) # Index in name list
138+
name_op('LOAD_GLOBAL', 116, 5) # Index in name list
139139
def_op('IS_OP', 117)
140140
def_op('CONTAINS_OP', 118)
141141
def_op('RERAISE', 119)
@@ -198,6 +198,7 @@ def jabs_op(name, op, entries=0):
198198
def_op('KW_NAMES', 172)
199199
hasconst.append(172)
200200

201+
201202
del def_op, name_op, jrel_op, jabs_op
202203

203204
_nb_ops = [

0 commit comments

Comments
 (0)