Skip to content

Commit d77d973

Browse files
authored
gh-105481: remove dependency of _inline_cache_entries on opname (#107339)
1 parent 766d251 commit d77d973

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

Include/internal/pycore_opcode.h

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

Lib/dis.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,16 @@ def show_code(co, *, file=None):
288288
_OPNAME_WIDTH = 20
289289
_OPARG_WIDTH = 5
290290

291+
def _get_cache_size(opname):
292+
return _inline_cache_entries.get(opname, 0)
293+
291294
def _get_jump_target(op, arg, offset):
292295
"""Gets the bytecode offset of the jump target if this is a jump instruction.
293296
294297
Otherwise return None.
295298
"""
296299
deop = _deoptop(op)
297-
caches = _inline_cache_entries[deop]
300+
caches = _get_cache_size(_all_opname[deop])
298301
if deop in hasjrel:
299302
if _is_backward_jump(deop):
300303
arg = -arg
@@ -353,7 +356,7 @@ def cache_offset(self):
353356
@property
354357
def end_offset(self):
355358
"""End index of the cache entries following the operation."""
356-
return self.cache_offset + _inline_cache_entries[self.opcode]*2
359+
return self.cache_offset + _get_cache_size(_all_opname[self.opcode])*2
357360

358361
@property
359362
def jump_target(self):
@@ -535,7 +538,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
535538
argrepr = ''
536539
positions = Positions(*next(co_positions, ()))
537540
deop = _deoptop(op)
538-
caches = _inline_cache_entries[deop]
541+
caches = _get_cache_size(_all_opname[deop])
539542
op = code[offset]
540543
if arg is not None:
541544
# Set argval to the dereferenced value of the argument when
@@ -679,7 +682,7 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
679682
else:
680683
# Each CACHE takes 2 bytes
681684
is_current_instr = instr.offset <= lasti \
682-
<= instr.offset + 2 * _inline_cache_entries[_deoptop(instr.opcode)]
685+
<= instr.offset + 2 * _get_cache_size(_all_opname[_deoptop(instr.opcode)])
683686
print(instr._disassemble(lineno_width, is_current_instr, offset_width),
684687
file=file)
685688
if exception_entries:
@@ -712,7 +715,7 @@ def _unpack_opargs(code):
712715
continue
713716
op = code[i]
714717
deop = _deoptop(op)
715-
caches = _inline_cache_entries[deop]
718+
caches = _get_cache_size(_all_opname[deop])
716719
if deop in hasarg:
717720
arg = code[i+1] | extended_arg
718721
extended_arg = (arg << 8) if deop == EXTENDED_ARG else 0

Lib/opcode.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,6 @@ def pseudo_op(name, op, real_ops):
347347
},
348348
}
349349

350-
_inline_cache_entries = [
351-
sum(_cache_format.get(opname[opcode], {}).values()) for opcode in range(256)
352-
]
350+
_inline_cache_entries = {
351+
name : sum(value.values()) for (name, value) in _cache_format.items()
352+
}

Lib/test/test__opcode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_specialization_stats(self):
106106
specialized_opcodes = [
107107
op.lower()
108108
for op in opcode._specializations
109-
if opcode._inline_cache_entries[opcode.opmap[op]]
109+
if opcode._inline_cache_entries.get(op, 0)
110110
]
111111
self.assertIn('load_attr', specialized_opcodes)
112112
self.assertIn('binary_subscr', specialized_opcodes)

Tools/build/generate_opcode_h.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ def main(opcode_py,
120120
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")
121121

122122
iobj.write("\nconst uint8_t _PyOpcode_Caches[256] = {\n")
123-
for i, entries in enumerate(opcode["_inline_cache_entries"]):
124-
if entries:
125-
iobj.write(f" [{opname[i]}] = {entries},\n")
123+
for name, entries in opcode["_inline_cache_entries"].items():
124+
iobj.write(f" [{name}] = {entries},\n")
126125
iobj.write("};\n")
127126

128127
deoptcodes = {}

0 commit comments

Comments
 (0)