Skip to content

Document how to add a new bytecode specialization - #1932

Draft
shreyash-bhosale wants to merge 3 commits into
python:mainfrom
shreyash-bhosale:document-bytecode-specialization
Draft

shreyash-bhosale wants to merge 3 commits into
python:mainfrom
shreyash-bhosale:document-bytecode-specialization

Conversation

@shreyash-bhosale

Copy link
Copy Markdown

Summary

Adds a new developer guide at developer-workflow/specialization.rst documenting how to specialize a bytecode instruction in CPython's Specializing Adaptive Interpreter (PEP 659).

Motivation

Adding a new bytecode specialization requires updates across several disparate subsystems in CPython, including the interpreter DSL in Python/bytecodes.c, inline cache layouts in Include/internal/pycore_code.h, specialization routines in Python/specialize.c, dis metadata in Lib/opcode.py, bytecode magic numbers, and code regeneration.

Currently, this knowledge is spread across past PRs and core developer memory.

Documenting this workflow in the Developer's Guide provides a clear, step-by-step checklist to lower the barrier for new contributors interested in interpreter optimization.

What's Included

The new document breaks down the process into an eight-step recipe, using [python/cpython#116385](python/cpython#116385) (specialization of CONTAINS_OP) as a concrete, real-world example:

  1. Inline Cache & Header Declarations (Include/internal/pycore_code.h): Struct definition with uint16_t counter, the CACHE_ENTRIES macro, and the function signature.
  2. Bytecode DSL & Instruction Families (Python/bytecodes.c): Defining family(), refactoring the base instruction to micro-op op(_OP), adding specializing op(_SPECIALIZE_OP), combining via macro(OP), and writing specialized inst(OP_VARIANT, (unused/N, ...)) cases with DEOPT_IF guards.
  3. Specialization Function & Statistics (Python/specialize.c): Registering statistics with add_stat_dict(), implementing type checks, rewriting instr->op.code, updating counter backoff/cooldown, and tracking STAT_INC hits and misses.
  4. Internal Fast-Path Helpers: Declaring type-specific C helpers in internal headers when bypassing generic slot dispatch.
  5. Disassembler Metadata (Lib/opcode.py): Adding the cache format to _inline_cache_entries for dis.
  6. Bytecode Cache Invalidation (Include/internal/pycore_magic_number.h): Bumping MAGIC_NUMBER.
  7. Code Generation: Running make regen-all or build.bat --regen.
  8. Testing & Benchmarking: Running opcode and disassembly tests, inspecting pystats, and benchmarking with pyperformance.

The new page is also registered in developer-workflow/index.rst.

Verification

  • Built the documentation locally using Sphinx:

    make html
@python-cla-bot

python-cla-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@StanFromIreland

Copy link
Copy Markdown
Member

We can't review unless the CLA is signed.

@StanFromIreland
StanFromIreland marked this pull request as draft October 31, 2026 09:27
@StanFromIreland

Copy link
Copy Markdown
Member

See also existing PR python/cpython#130831.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants