Skip to content

gh-105481: remove dependency of _inline_cache_entries on opname #107339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,16 @@ def show_code(co, *, file=None):
_OPNAME_WIDTH = 20
_OPARG_WIDTH = 5

def _get_cache_size(opname):
return _inline_cache_entries.get(opname, 0)

def _get_jump_target(op, arg, offset):
"""Gets the bytecode offset of the jump target if this is a jump instruction.

Otherwise return None.
"""
deop = _deoptop(op)
caches = _inline_cache_entries[deop]
caches = _get_cache_size(_all_opname[deop])
if deop in hasjrel:
if _is_backward_jump(deop):
arg = -arg
Expand Down Expand Up @@ -353,7 +356,7 @@ def cache_offset(self):
@property
def end_offset(self):
"""End index of the cache entries following the operation."""
return self.cache_offset + _inline_cache_entries[self.opcode]*2
return self.cache_offset + _get_cache_size(_all_opname[self.opcode])*2

@property
def jump_target(self):
Expand Down Expand Up @@ -535,7 +538,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
argrepr = ''
positions = Positions(*next(co_positions, ()))
deop = _deoptop(op)
caches = _inline_cache_entries[deop]
caches = _get_cache_size(_all_opname[deop])
op = code[offset]
if arg is not None:
# Set argval to the dereferenced value of the argument when
Expand Down Expand Up @@ -679,7 +682,7 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
else:
# Each CACHE takes 2 bytes
is_current_instr = instr.offset <= lasti \
<= instr.offset + 2 * _inline_cache_entries[_deoptop(instr.opcode)]
<= instr.offset + 2 * _get_cache_size(_all_opname[_deoptop(instr.opcode)])
print(instr._disassemble(lineno_width, is_current_instr, offset_width),
file=file)
if exception_entries:
Expand Down Expand Up @@ -712,7 +715,7 @@ def _unpack_opargs(code):
continue
op = code[i]
deop = _deoptop(op)
caches = _inline_cache_entries[deop]
caches = _get_cache_size(_all_opname[deop])
if deop in hasarg:
arg = code[i+1] | extended_arg
extended_arg = (arg << 8) if deop == EXTENDED_ARG else 0
Expand Down
6 changes: 3 additions & 3 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,6 @@ def pseudo_op(name, op, real_ops):
},
}

_inline_cache_entries = [
sum(_cache_format.get(opname[opcode], {}).values()) for opcode in range(256)
]
_inline_cache_entries = {
name : sum(value.values()) for (name, value) in _cache_format.items()
}
2 changes: 1 addition & 1 deletion Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_specialization_stats(self):
specialized_opcodes = [
op.lower()
for op in opcode._specializations
if opcode._inline_cache_entries[opcode.opmap[op]]
if opcode._inline_cache_entries.get(op, 0)
]
self.assertIn('load_attr', specialized_opcodes)
self.assertIn('binary_subscr', specialized_opcodes)
Expand Down
5 changes: 2 additions & 3 deletions Tools/build/generate_opcode_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ def main(opcode_py,
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")

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

deoptcodes = {}
Expand Down