FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-96793: Specialize FOR_ITER for generators. by markshannon · Pull Request #98772 · python/cpython · GitHub

/ cpython Public

GH-96793: Specialize FOR_ITER for generators. - #98772

Merged
markshannon merged 7 commits into
python:mainfrom
faster-cpython:specialize-for-iter-gen
Nov 7, 2022
Merged

GH-96793: Specialize FOR_ITER for generators.#98772
markshannon merged 7 commits into
python:mainfrom
faster-cpython:specialize-for-iter-gen

Conversation

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

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.

Copy link
Copy Markdown
Member Author

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

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

frame->frame_obj = NULL;
frame->prev_instr = _PyCode_CODE(code) - 1;
frame->is_entry = false;
frame->yield_offset = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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).

markshannon commented Nov 1, 2022
edited
Loading

Copy link
Copy Markdown
Member Author

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

markshannon commented Nov 7, 2022
edited
Loading

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.

Comment thread Lib/test/test_generators.py Outdated
Comment thread Lib/test/test_generators.py Outdated
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL