Skip to content

Conversation

faizanoor3001
Copy link

Solves: python/devguide#1286

Description

  • Add a "How to Add a New Bytecode Specialization” section in the InternalDocs/interpreter.md file.
    • This guide explains how to add a new bytecode specialization, using
      CONTAINS_OP as an example.

PS: Initial PR for reference: python/devguide#1522


📚 Documentation preview 📚: https://cpython-devguide--1522.org.readthedocs.build/

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news awaiting review labels Mar 4, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Mar 4, 2025
@faizanoor3001 faizanoor3001 changed the title gh-1286 : Document how to add a bytecode specialization in Interpreter.md file python/devguide#1286 : Document how to add a bytecode specialization in Interpreter.md file Mar 4, 2025
@faizanoor3001 faizanoor3001 changed the title python/devguide#1286 : Document how to add a bytecode specialization in Interpreter.md file gh-1286 : Document how to add a bytecode specialization in Interpreter.md file Mar 4, 2025
Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Look good mostly. Just a few minor edits.


3. Write the specializing function itself in [Python/specialize.c ](../Python/specialize.c).
Refer to any other function in that file for the format.
4. Remember to update operation stats by calling add_stat_dict in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
4. Remember to update operation stats by calling add_stat_dict in
4. Remember to update operation stats by calling `add_stat_dict` in

@Fidget-Spinner Fidget-Spinner changed the title gh-1286 : Document how to add a bytecode specialization in Interpreter.md file Document how to add a bytecode specialization in Interpreter.md file Mar 4, 2025
Copy link
Member

@iritkatriel iritkatriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few tweak/suggestions

Comment on lines 528 to 529
- Add a uop that calls the specializing function `_SPECIALIZE_CONTAINS_OP`.
For example.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Add a uop that calls the specializing function `_SPECIALIZE_CONTAINS_OP`.
For example.
- Add a uop that calls the specializing function:

Comment on lines 545 to 546
- The original `CONTAINS_OP` is now a new macro consisting of
`_SPECIALIZE_CONTAINS_OP` and `_CONTAINS_OP`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a macro for the original bytecode name:

    macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP;

Comment on lines 557 to 565
3. Write the specializing function itself in [Python/specialize.c ](../Python/specialize.c).
Refer to any other function in that file for the format.
4. Remember to update operation stats by calling `add_stat_dict` in
[Python/specialize.c ](../Python/specialize.c).
5. Add the cache layout in [Lib/opcode.py](../Lib/opcode.py) so that Python's
dis module will know how to represent it properly.
6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
3. Write the specializing function itself in [Python/specialize.c ](../Python/specialize.c).
Refer to any other function in that file for the format.
4. Remember to update operation stats by calling `add_stat_dict` in
[Python/specialize.c ](../Python/specialize.c).
5. Add the cache layout in [Lib/opcode.py](../Lib/opcode.py) so that Python's
dis module will know how to represent it properly.
6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows.
3. Write the specializing function itself (_Py_Specialize_ContainsOp) in [Python/specialize.c ](../Python/specialize.c).
Refer to other functions in that file for the pattern.
4. Add a call to `add_stat_dict` in `_Py_GetSpecializationStats` which is in
[Python/specialize.c ](../Python/specialize.c).
5. Add the cache layout in [Lib/opcode.py](../Lib/opcode.py) so that Python's
`dis` module will know how to represent it properly.
6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
7. Run `make regen-all` on `*nix` or `build.bat --regen` on Windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion has some edits to the text as well, not just whitespace.

@iritkatriel
Copy link
Member

Let's give @brandtbucher a chance to review as well.

Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct. A couple of possible minor improvements, if we're aiming to be comprehensive here:

6. Bump magic number in [Include/core/pycore_magic_number.h](../Include/internal/pycore_magic_number.h).
7. Run ``make regen-all`` on `*nix` or `build.bat --regen` on Windows.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the only thing missing is adding an actual specialized variant. Maybe that's implied/obvious, but it wouldn't hurt to provide a dumb example of _Py_Specialize_ContainsOp and _CONTAINS_OP_UNICODE_UNICODE that just guards and calls PyUnicode_Contains or something.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several grammar nitpicks--English is hard!

@hugovk
Copy link
Member

hugovk commented Oct 14, 2025

I've applied the suggestions. Ready to merge? And backport?

Co-authored-by: Ken Jin <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Brandt Bucher <[email protected]>
@hugovk hugovk requested a review from AA-Turner as a code owner October 14, 2025 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

6 participants