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-100762: Switch order of exception match in gen_close by eendebakpt · Pull Request #101011 · python/cpython

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Jan 13, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This PR optimizes the closing of generators by switching the match statements in the expression PyErr_ExceptionMatches(PyExc_StopIteration) || PyErr_ExceptionMatches(PyExc_GeneratorExit) from the gen_close method. The case of a PyExc_GeneratorExit is more common.

Benchmark (with optimizations including PGO) updated May 9th:

all: Mean +- std dev: [main2] 283 ns +- 11 ns -> [pr] 280 ns +- 6 ns: 1.01x faster
all with comparison: Mean +- std dev: [main2] 570 ns +- 14 ns -> [pr] 563 ns +- 9 ns: 1.01x faster

Benchmark hidden because not significant (3): generator, list comprehension, generator exhaust

Geometric mean: 1.00x faster

Benchmark script:

import pyperf
runner = pyperf.Runner()

setup="""
l=[1,2]
"""

runner.timeit(name=f"generator", stmt=f"(x for x in l)", setup=setup)
runner.timeit(name=f"list comprehension", stmt=f"[x for x in l]", setup=setup)
stmt="""
for x in (_ for _ in ()):
    pass
"""
runner.timeit(name=f"generator exhaust", stmt=stmt, setup=setup)
runner.timeit(name=f"all", stmt=f"all( (x for x in l) ) ", setup=setup)
runner.timeit(name=f"all with comparison", stmt=f"all( (x==1 for x in l) ) ", setup=setup)

Notes:

  • Also see Generator finalization is slower in 3.11 vs 3.10 #100762 (comment)
  • The cases for which the PyExc_StopIteration is triggered occur less than the PyExc_GeneratorExit. Cases from the python test suite are test_async_gen_asyncio_aclose_10 in test_asyncgen.py and the following code from test_generators.py:
def f():
    try: 
        yield None
        print('here')
    except GeneratorExit:
        print("exiting")
    print('done')

g = f()
next(g)
g.close()
  • Issue: Generator finalization is slower in 3.11 vs 3.10 #100762

Sorry, something went wrong.

Uh oh!

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

All reactions
@eendebakpt
switch order of exception match in gen_close
8d4605e
@eendebakpt
eendebakpt requested a review from markshannon as a code owner January 13, 2023 10:50
@bedevere-bot bedevere-bot added the awaiting review label Jan 13, 2023
@bedevere-bot bedevere-bot mentioned this pull request Jan 13, 2023
Generator finalization is slower in 3.11 vs 3.10 #100762
Closed
@markshannon markshannon mentioned this pull request Jan 13, 2023
GH-100762: Don't call gen.throw() in gen.close(), unless necessary. #101013
Merged
eendebakpt added 5 commits January 18, 2023 16:02
@eendebakpt
Merge branch 'main' into gen_close
6c0cef2
@eendebakpt
Merge branch 'main' into gen_close
082fd34
@eendebakpt
Merge branch 'main' into gen_close
5efbf95
@eendebakpt
Merge branch 'main' into gen_close
8df595b
@eendebakpt
Merge branch 'gen_close' of github.com:eendebakpt/cpython into gen_close
dbc8398
@brandtbucher brandtbucher added the skip news label May 10, 2023
brandtbucher
brandtbucher approved these changes May 10, 2023
View reviewed changes

@brandtbucher brandtbucher left a comment

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.

Seems good. Let's wait for 3.13 in two weeks, since performance improvements are effectively frozen right now.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels May 10, 2023
@brandtbucher brandtbucher self-assigned this May 10, 2023
@eendebakpt

eendebakpt commented Jun 9, 2023

Copy link
Copy Markdown
Contributor Author

Seems good. Let's wait for 3.13 in two weeks, since performance improvements are effectively frozen right now.

@brandtbucher Thanks for looking into this. The main branch has changed in the meantime, and there is currently only a single match instead of the two before. This makes this PR irrelevant, I will close it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@eendebakpt eendebakpt closed this Jun 9, 2023
@eendebakpt
eendebakpt deleted the gen_close branch June 26, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@brandtbucher brandtbucher brandtbucher approved these changes
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

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

Assignees

@brandtbucher brandtbucher

Labels

awaiting merge skip news

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.

3 participants

@eendebakpt @brandtbucher @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.