GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Optimize reference tracking and eliminate branching during returns and yields · Issue #144540 · python/cpython

Optimize reference tracking and eliminate branching during returns and yields #144540

New issue
Copy link
New issue
Copy link
Open
Open
Optimize reference tracking and eliminate branching during returns and yields#144540
Copy link
Labels
3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-JIT

Description

@markshannon
markshannon
opened on Feb 6, 2026
Issue body actions

There are a few optimizations we can make to returns and yields, but they are somewhat related so I'm grouping them into a single issue.

During a return, the VM needs to make the returned reference "heap safe" (convert any borrowed references to strong references) and then pop and clear the frame, which can involve quite a lot of decrefs.
During a yield, the VM needs to make the returned reference heap safe and then pop the frame, but not clear it.

We want to minimize the amount of refcounting operations that we do.

Before we can do much else, we should split RETURN_VALUE and YIELD_VALUE into micro-ops:

macro(RETURN_VALUE) = _MAKE_HEAP_SAFE + _RETURN_VALUE

macro(YIELD_VALUE) = _MAKE_HEAP_SAFE + _YIELD_VALUE

so that we can optimize the uops independently

Eliminate making heap safe if we know that reference already is

If TOS is a strong reference _MAKE_HEAP_SAFE -> _NOP

Avoid the branch in _RETURN_VALUE by splitting into _RETURN_VALUE_GEN and _RETURN_VALUE_FUNC

To avoid using up opcodes, we'll need to do this in the JIT, not the interpreter.

Track borrows/immortals to optimize clearing frames

E.g. if a frame has four local variables, a, b, c, d but we can tell that a and d are immortal, or borrowed, then instead of looping over all the variables, we could emit code just to decref b and c.
This might end up bloating the code, as we would need to inline the decrefs, but it could be quite a lot faster.

Linked PRs

  • gh-144540: Add _MAKE_HEAP_SAFE uop to eliminate unnecessary refcount operations in RETURN_VALUE and YIELD_VALUE #144414
  • gh-144540: Eliminate _MAKE_HEAP_FUNCTION for owned references #146320

Activity

  1. markshannon
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-JIT
    3.15bugs and security fixesbugs and security fixes
    on Feb 6, 2026
  2. markshannon
    added a commit that references this issue on Mar 11, 2026

    gh-144540: Add _MAKE_HEAP_SAFE uop to eliminate unnecessary refcount …

    f062014
  3. Fidget-Spinner commented on Mar 15, 2026

    @Fidget-Spinner
    Fidget-Spinner
    on Mar 15, 2026
    Member
    More actions

    @cocolato I suspect we can do more with _MAKE_HEAP_SAFE, e.g. if we have the following (return of a return)

    _MAKE_HEAP_SAFE
    _RETURN_VALUE
    _MAKE_HEAP_SAFE    <----- this can be eliminated!
    _RETURN_VALUE
    

    This shows up commonly in the bm_generators benchmark in pyperformance.

    Reacted by Hai Zhu
  4. Fidget-Spinner commented on Mar 22, 2026

    @Fidget-Spinner
    Fidget-Spinner
    on Mar 22, 2026
    Member
    More actions

    This shows up commonly in the bm_generators benchmark in pyperformance.

    @Sacul0457 would you like to work on this? It's just adding this conditional in _MAKE_HEAP_SAFE:

    +        if (sym_is_immortal(PyJitRef_Unwrap(value)) || !PyJitRef_IsBorrowed(value)) {
    

    And adding a test.

  5. Fidget-Spinner commented on Mar 22, 2026

    @Fidget-Spinner
    Fidget-Spinner
    on Mar 22, 2026
    Member
    More actions

    @cocolato if you don't mind, I'm sensing you're a little busier now, so I will let Sacul take over for this one. I have some very interesting problems for you in the future :). Thanks a lot for all the work you've done so far!

    Reacted by Hai Zhu
  6. cocolato commented on Mar 22, 2026

    @cocolato
    cocolato
    on Mar 22, 2026
    Member
    More actions

    @cocolato if you don't mind, I'm sensing you're a little busier now, so I will let Sacul take over for this one. I have some very interesting problems for you in the future :). Thanks a lot for all the work you've done so far!

    Never mind. I’ve been little busy because I’m in the middle of a job handover, but I should have plenty of time starting in early April. I’ll also look into some of the existing issues then. If there's anything I can do to help, please let me know!

    Reacted by Ken Jin and Sacul
  7. Sacul0457 commented on Mar 22, 2026

    @Sacul0457
    Sacul0457
    on Mar 22, 2026
    Contributor
    More actions

    Thank you both! I'll look into this in a few hours!

    Reacted by Hai Zhu
  8. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-144540: Add _MAKE_HEAP_SAFE uop to eliminate unnecessary ref…

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-JIT

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.