You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document how to add a bytecode specialization #1286
How to add a new bytecode specialization is knowledge only a few of us remember right now. We should document this somewhere (devguide or CPython, I don't mind). Hopefully a new contributor can pick this up!
Assuming you found an instruction that serves as a good specialization candidate. Let's use the example of CONTAINS_OP:
Set the original CONTAINS_OP to a uop by changing it from CONTAINS_OP to _CONTAINS_OP in Python/bytecodes.c. The instruction definition should also change from inst to op.
Add the uop that calls a specializing function. In this case _SPECIALIZE_CONTAINS_OP.
The original CONTAINS_OP is now a new macro consisting of _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP.
Define the cache structure in Include/internal/pycore_code.h. At the very least, a 16-bit counter is needed.
Write the specializing function itself in Python/specialize.c. Refer to any other function in that file for the format.
Remember to update operation stats by calling add_stat_dict in Python/specialize.c.
Add the cache layout in Lib/opcode.py so that Python's dis module will know how to represent it properly.
Bump magic number in Include/internal/pycore_magic_number.h.
Run make regen-all on *nix or build.bat --regen on PC.
Hello, @Fidget-Spinner, I’ve opened a PR adding “How to Add a New Bytecode Specialization” to the How-To section. We can verify the text changes or if any updates are required, but I’m unsure about the best place for this new guide. Would it fit better in:
PS: This is my first contribution, so I appreciate your guidance. I’m also studying the Crafting Interpreters book, and your original PR helped me delve deeper into the interpreter’s workings. Thank you!
@Fidget-Spinner Raised a PR, but since the issue is in devguide project and the PR I have raised in the cpython project, the bot is unable to recognize the gh-NNNN issue number in this case gh-1286 as correct issue number, could you please help here, as I guess we would need a new issue number ? Or how to work around on this ?
My name is Nayan Raj, and I have recently started contributing to organizations that participate in Google Summer of Code (GSoC). This is my first experience contributing to a GSoC organization, although I have previously worked on other open-source projects and have some contribution experience.
I would greatly appreciate any guidance, suggestions, or feedback that could help me improve my contributions and better understand the GSoC contribution process. I am eager to learn, enhance my skills, and become a valuable contributor to the community.
Thank you for your time and support. I look forward to learning from your experience and contributing effectively to the project.
please assign this issue to me
Activity
AbudiMutamba commentedon Oct 10, 2024
Hey I would love to work on this issue if you don't mind, thanks,
willingc commentedon Oct 10, 2024
Hi @AbudiMutamba. Welcome 👋 Before you get too far along on this. Let's check with @Fidget-Spinner if this is still something that is needed?
Fidget-Spinner commentedon Oct 10, 2024
Yes we still need this.
willingc commentedon Oct 10, 2024
Thanks @Fidget-Spinner. If you want to give this a try @AbudiMutamba, please do so.
faizanoor3001 commentedon Feb 20, 2025
Hello, @willingc is it something still open ? Would like to contribute. :)
Fidget-Spinner commentedon Feb 20, 2025
@faizanoor3001 this is still open
faizanoor3001 commentedon Feb 24, 2025
Hello, @Fidget-Spinner, I’ve opened a PR adding “How to Add a New Bytecode Specialization” to the How-To section. We can verify the text changes or if any updates are required, but I’m unsure about the best place for this new guide. Would it fit better in:
Interpreter.md section - appended under the existing InternalDocs?
CPython Internals — as its own subsection?
Current:

Potential Alternative:
PS: This is my first contribution, so I appreciate your guidance. I’m also studying the Crafting Interpreters book, and your original PR helped me delve deeper into the interpreter’s workings. Thank you!
Fidget-Spinner commentedon Feb 24, 2025
@faizanoor3001 thanks for taking this up! I will review it soon.
faizanoor3001 commentedon Mar 4, 2025
@Fidget-Spinner Raised a PR, but since the issue is in devguide project and the PR I have raised in the cpython project, the bot is unable to recognize the gh-NNNN issue number in this case gh-1286 as correct issue number, could you please help here, as I guess we would need a new issue number ? Or how to work around on this ?
StanFromIreland commentedon Feb 21, 2026
In general we've removed the internals sections here, maybe this is better suited for InternalDocs?
nayanraj864-cmyk commentedon Sep 8, 2026
@Fidget-Spinner
Hello,
My name is Nayan Raj, and I have recently started contributing to organizations that participate in Google Summer of Code (GSoC). This is my first experience contributing to a GSoC organization, although I have previously worked on other open-source projects and have some contribution experience.
I would greatly appreciate any guidance, suggestions, or feedback that could help me improve my contributions and better understand the GSoC contribution process. I am eager to learn, enhance my skills, and become a valuable contributor to the community.
Thank you for your time and support. I look forward to learning from your experience and contributing effectively to the project.
please assign this issue to me
Best regards,
Nayan Raj