Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-100762: Switch order of exception match in gen_close#101011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
eendebakpt wants to merge6 commits intopython:mainfromeendebakpt:gen_close

Conversation

@eendebakpt
Copy link
Contributor

@eendebakpteendebakpt commentedJan 13, 2023
edited
Loading

This PR optimizes the closing of generators by switching the match statements in the expressionPyErr_ExceptionMatches(PyExc_StopIteration) || PyErr_ExceptionMatches(PyExc_GeneratorExit) from thegen_close method. The case of aPyExc_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 fasterall with comparison: Mean +- std dev: [main2] 570 ns +- 14 ns -> [pr] 563 ns +- 9 ns: 1.01x fasterBenchmark hidden because not significant (3): generator, list comprehension, generator exhaustGeometric mean: 1.00x faster

Benchmark script:

import pyperfrunner = 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:

def f():    try:         yield None        print('here')    except GeneratorExit:        print("exiting")    print('done')g = f()next(g)g.close()

Copy link
Member

@brandtbucherbrandtbucher left a 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.

@eendebakpt
Copy link
ContributorAuthor

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.

@eendebakpteendebakpt deleted the gen_close branchJune 26, 2025 13:45
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@brandtbucherbrandtbucherbrandtbucher approved these changes

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

Assignees

@brandtbucherbrandtbucher

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@eendebakpt@brandtbucher@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp