Skip to content

Specialize FOR_ITER for map() with a Python function #162463

Open

Description

@eendebakpt

Feature or enhancement

for x in map(func, iterable) with a Python func makes a Python → C → Python round trip on every item: FOR_ITER calls map.__next__, which calls func through vectorcall and re-enters the interpreter. A FOR_ITER_MAP specialization can instead fetch the next item from the map's inner iterator and push the frame of func directly, the way FOR_ITER_GEN pushes the generator frame. The return value of func becomes the loop item.

The specialization applies when the iterator is an exact map object with a single iterable and its function is a plain Python function. Everything else keeps the current path.

A prototype (about 60 lines in bytecodes.c and specialize.c) gives 1.2–1.3x on such loops and comprehensions, with no change for C callables.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Activity

  1. added
    type-featureA feature request or enhancement
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Nov 22, 2026
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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions