]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105481: remove dependency of _inline_cache_entries on opname (#107339)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Thu, 27 Jul 2023 13:15:25 +0000 (14:15 +0100)
committerGitHub <noreply@github.com>
Thu, 27 Jul 2023 13:15:25 +0000 (14:15 +0100)
Include/internal/pycore_opcode.h
Lib/dis.py
Lib/opcode.py
Lib/test/test__opcode.py
Tools/build/generate_opcode_h.py

index d7f6b84e95c4f8478911f37b369f479d9a8b7a34..aff09a2a926aecede6383e22654f8bdb23a10d02 100644 (file)
@@ -19,20 +19,20 @@ extern const uint8_t _PyOpcode_Deopt[256];
 #ifdef NEED_OPCODE_TABLES
 
 const uint8_t _PyOpcode_Caches[256] = {
-    [TO_BOOL] = 3,
-    [BINARY_SUBSCR] = 1,
-    [STORE_SUBSCR] = 1,
+    [LOAD_GLOBAL] = 4,
+    [BINARY_OP] = 1,
     [UNPACK_SEQUENCE] = 1,
+    [COMPARE_OP] = 1,
+    [BINARY_SUBSCR] = 1,
     [FOR_ITER] = 1,
-    [STORE_ATTR] = 4,
+    [LOAD_SUPER_ATTR] = 1,
     [LOAD_ATTR] = 9,
-    [COMPARE_OP] = 1,
-    [LOAD_GLOBAL] = 4,
-    [BINARY_OP] = 1,
+    [STORE_ATTR] = 4,
+    [CALL] = 3,
+    [STORE_SUBSCR] = 1,
     [SEND] = 1,
     [JUMP_BACKWARD] = 1,
-    [LOAD_SUPER_ATTR] = 1,
-    [CALL] = 3,
+    [TO_BOOL] = 3,
 };
 
 const uint8_t _PyOpcode_Deopt[256] = {
index 184eb14a5b2b21eb7fd34aeeaa7f6d9100df95a1..b167773dfe7b0cd4a088c8ea708d77bf01ca975c 100644 (file)
@@ -288,13 +288,16 @@ _ExceptionTableEntry = collections.namedtuple("_ExceptionTableEntry",
 _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
@@ -353,7 +356,7 @@ class Instruction(_Instruction):
     @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):
@@ -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
@@ -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:
@@ -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
index 36831d8a122bff758390ddaa500b6a64b1ede83d..bed922399821a63bc2115eef362272be87c3431c 100644 (file)
@@ -347,6 +347,6 @@ _cache_format = {
     },
 }
 
-_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()
+}
index b3a9bcbe160453d43cccb9b90bb50160caa445eb..c1f612dc4a63cb6484816448f5458cf4b57cf4cb 100644 (file)
@@ -106,7 +106,7 @@ class SpecializationStatsTests(unittest.TestCase):
         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)
index 179abcf989e9bf2dc38ff1c963c0dc26c779dda3..19e5eab822a235d1e3bec3bb27e611d24b581a33 100644 (file)
@@ -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 = {}