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 — GH-96793: Specialize FOR_ITER for generators. by markshannon · Pull Request #98772 · python/cpython

Conversation

@markshannon

@markshannon markshannon commented Oct 27, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Performance results seem to be just noise. I suspect there aren't enough uses of generators in the benchmark suite for this to make a difference.

  • Issue: Enable specialization of iteration over generators, for gen():, and awaiting coroutines, await coro() #96793

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markshannon
Specialize FOR_ITER for generators.
3d229cc
@bedevere-bot bedevere-bot added the awaiting core review label Oct 27, 2022
@markshannon markshannon mentioned this pull request Oct 28, 2022
Enable specialization of iteration over generators, for gen():, and awaiting coroutines, await coro() #96793
Closed
4 tasks done
@markshannon
Add news
d8e88b0
@markshannon

markshannon commented Oct 31, 2022

Copy link
Copy Markdown
Member Author

There is a bug in this. It doesn't set the gi_exc_state stack.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Oct 31, 2022

Copy link
Copy Markdown
Member Author

https://github.com/python/cpython/compare/main...faster-cpython:cpython:specialize-for-iter-gen-handle-exc-stack?expand=1
fixes the gi_exc_state issue, but is messy. I think this would benefit from merging #96319 first

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed Nov 1, 2022
View reviewed changes
Comment thread Include/internal/pycore_frame.h
frame->frame_obj = NULL;
frame->prev_instr = _PyCode_CODE(code) - 1;
frame->is_entry = false;
frame->yield_offset = 0;

@iritkatriel iritkatriel Nov 1, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Why is this called "yield_offset", is it not the relative offset to jump by when the generator is exhausted? (the same as the arg to FOR_ITER?)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markshannon markshannon Nov 1, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Because it's the offset for YIELD_VALUE to return to relative to RETURN_VALUE
https://github.com/python/cpython/pull/98772/files#diff-c22186367cbe20233e843261998dc027ae5f1f8c0d2e778abfa454ae74cc59deR2087

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kumaraditya303

kumaraditya303 commented Nov 1, 2022

Copy link
Copy Markdown
Contributor

I suspect there aren't enough uses of generators in the benchmark suite for this to make a difference.

You can run https://github.com/python/pyperformance/blob/main/pyperformance/data-files/benchmarks/bm_generators/run_benchmark.py, it is specifically designed to benchmark generators (microbenchmark).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Nov 1, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I'll run that benchmark if pyperformance ever does another release.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

markshannon added 2 commits November 4, 2022 14:39
@markshannon
Merge branch 'main' into specialize-for-iter-gen
e16d82c
@markshannon
Clean up after merge.
86aa1a8
@markshannon

markshannon commented Nov 7, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I'm seeing a 35% speedup on this benchmark:

Tree iterator
import timeit

class Tree:
    def __init__(self, left, value, right):
        self.left = left
        self.value = value
        self.right = right


    def __iter__(self):
        if self.left:
            for item in self.left:
                yield item
        yield self.value
        if self.right:
            for item in self.right:
                yield item

def tree(input: range) -> Tree | None:
    n = len(input)
    if n == 0:
        return None
    i = n // 2
    return Tree(tree(input[:i]), input[i], tree(input[i + 1:]))

def setup():
    global iterable
    assert list(tree(range(10))) == list(range(10))
    iterable = tree(range(100000))
    
print(timeit.timeit("for _ in iterable: pass", "setup()", globals=globals(), number=10))

Note that this uses yield not yield from. yield from compiles to the SEND instruction which is for another PR.


And a 36% speedup on this one:

Flat iterator
import timeit

class RangeWrapper:
    def __init__(self, n):
        self.r = range(n)

    def __iter__(self):
        for item in self.r:
            yield item

def setup():
    global iterable
    iterable = RangeWrapper(1000000)

print(timeit.timeit("for _ in iterable: pass", "setup()", globals=globals(), number=10))

Comparing the fastest of 6 runs for main and this PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel approved these changes Nov 7, 2022
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Nov 7, 2022
markshannon added 2 commits November 7, 2022 13:47
@markshannon
Add test for nested exc_info in loop.
a677191
@markshannon
Handle exc stack.
8256be6
iritkatriel
iritkatriel reviewed Nov 7, 2022
View reviewed changes
Comment thread Lib/test/test_generators.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed Nov 7, 2022
View reviewed changes
Comment thread Lib/test/test_generators.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@markshannon @iritkatriel
Use new API for current exception
8fb80ca
Co-authored-by: Irit Katriel <[email protected]>
@markshannon
markshannon merged commit 4a1c58d into python:main Nov 7, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Nov 7, 2022
@mdboom mdboom mentioned this pull request Feb 14, 2023
pylint is broken with CPython main #101914
Closed
@markshannon
markshannon deleted the specialize-for-iter-gen branch September 26, 2023 12:50
@cdce8p cdce8p mentioned this pull request Feb 20, 2024
Python 3.12 - ValueError: generator already executing pylint-dev/pylint#9138
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iritkatriel iritkatriel iritkatriel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@markshannon @kumaraditya303 @iritkatriel @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.