Back
+20 XP
Concept

Disassembling Bytecode: dis & Opcode Super-Instructions

CPython executes a virtual stack machine. The `dis` module decompiles bytecode into human-readable instructions. Python 3.13 introduces super-instructions (like `LOAD_FAST_LOAD_FAST`) that fuse adjacent operations to eliminate interpreter evaluation loop overhead 🔍⚙️.

python
import dis

def compute(a, b):
    return (a + b) * 2

# Decompile bytecode instructions:
print("Bytecode disassembly:")
dis.dis(compute)

# Inspect opcodes programmatically:
opnames = [i.opname for i in dis.get_instructions(compute)]
print(f"Opcodes emitted: {opnames} 🚀")

# 85. Disassembling Bytecode: dis & Opcode Super-Instructions Python does not execute raw source text. When you run a script, CPython compiles the AST into bytecode instructions executed by `Python/ceval.c`—a giant C `switch` statement running on a virtual evaluation stack 🛑. ```python # The Stack Machine Model: # 1. LOAD_FAST x -> Pushes x onto top of stack (TOS) # 2. LOAD_CONST 10 -> Pushes 10 onto top of stack # 3. BINARY_OP (ADD) -> Pops x and 10, adds them, pushes sum onto stack # 4. RETURN_VALUE -> Pops sum and returns to caller! ``` ### Python 3.13 Super-Instructions 1. **Instruction Fusion**: In older Python versions, passing `(a, b)` generated two separate `LOAD_FAST` instructions, requiring two trips through the C interpreter dispatch loop. Python 3.13 fuses them into `LOAD_FAST_LOAD_FAST`, pushing both values in a single cycle 🏎️. 2. **Specialized Caches**: Instructions like `BINARY_OP` and `LOAD_ATTR` are followed by cache entries (seen in `dis.dis(..., show_caches=True)`), storing type pointers to skip dictionary lookups for monomorphic calls. 3. **Optimizing Inner Loops**: By disassembling performance-critical loops with `dis`, senior engineers spot accidental global lookups (`LOAD_GLOBAL` instead of `LOAD_FAST`) and eliminate unnecessary boxing overhead 🔍⚙️. --- ### Interactive Pop-Quiz 🧠 #### Why did Python 3.13 introduce combined super-instructions like LOAD_FAST_LOAD_FAST into the bytecode compiler? - [ ] To push multiple variables onto the evaluation stack in a single dispatch step, reducing C interpreter loop overhead 🏎️ - [ ] To encrypt the bytecode against reverse engineering - [ ] To allocate additional swap space on the physical SSD - [ ] To force the variables to be stored as 16-bit floating point numbers --- ### Practical Challenge 🎯 Disassemble a function `def get_value(x): return x`. Extract all opcode names using `dis.get_instructions()`. Assert that `'LOAD_FAST'` and `'RETURN_VALUE'` are in the extracted opcode list. Print the list of opcodes. <details> <summary>Solution</summary> ```python import dis def get_value(x): return x opcodes = [instr.opname for instr in dis.get_instructions(get_value)] assert "LOAD_FAST" in opcodes assert "RETURN_VALUE" in opcodes print(opcodes) ``` </details>

Checkpoint

Why did Python 3.13 introduce combined super-instructions like `LOAD_FAST_LOAD_FAST` into the bytecode compiler? 🧠

Practice

Disassemble a function `def get_value(x): return x`. Extract all opcode names using `dis.get_instructions()`. Assert that `'LOAD_FAST'` and `'RETURN_VALUE'` are in the extracted opcode list. Print the list of opcodes. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks