Skip to content

[Idea] Perform the first SWAP under LOAD_FAST_AND_CLEAR #136833

@gkirchou

Description

@gkirchou

Bug report

Bug description:

In PEP 709, for the given example:

def f(lst):
    return [x for x in lst]

The bytecode is generated this way:

1           0 RESUME                   0

2           2 LOAD_FAST                0 (lst)
            4 GET_ITER
            6 LOAD_FAST_AND_CLEAR      1 (x)
            8 SWAP                     2
           10 BUILD_LIST               0
...

FWIK, the first SWAP after LOAD_FAST_AND_CLEAR is used to perform the original implicitly passed .0 in co_varnames (the iterated target passed from the MAKE_FUNCTION's caller before PEP 709).

And this seems to 100% be the needed case (I'm not sure, but I suspect it should be) when LOAD_FAST_AND_CLEAR is performed.

This makes me wonder if it's possible to merge the first SWAP operation under LOAD_FAST_AND_CLEAR, this should provide a negligible performance gain + bytecode operation simplification.

(I've verified this with under MacOS 15.5 (24F74) + Python 3.13.3, this should be something reproducible after 3.12.)


More background (an idea from a talk I gave in EuroPython 2025): https://github.com/note35/about/blob/dev/public/slides/2025-bytecode.pdf

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy