Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-100762: Don't call gen.throw() in gen.close(), unless necessary. #101013

Merged
merged 7 commits into from
Jan 24, 2023

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Jan 13, 2023

Change oparg of YIELD_VALUE to hold the exception handler depth.
We can then test that in gen.close() to avoid calling gen.throw() unless necessary.

@markshannon
Copy link
Member Author

markshannon commented Jan 13, 2023

Using the benchmarks in #101011 (comment), but using timeit instead of pyperf and a non-pgo and no-lto build because I'm impatient.
I get no significant change on "comprehension", "generator exhaust", and "all", but runtime is about 60% for "generator" and "all with comparison"

Lib/importlib/_bootstrap_external.py Outdated Show resolved Hide resolved
Doc/library/dis.rst Outdated Show resolved Hide resolved
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

I guess this will not be easy to backport, as we won't want to change the meaning of oparg in 3.11? Then again it appears to be unused except for the assert. Maybe it should just be a bit indicating whether it's safe to optimize out the throw?

Objects/genobject.c Show resolved Hide resolved
Python/compile.c Show resolved Hide resolved
Objects/genobject.c Show resolved Hide resolved
Lib/importlib/_bootstrap_external.py Outdated Show resolved Hide resolved
@brandtbucher
Copy link
Member

I guess this will not be easy to backport, as we won't want to change the meaning of oparg in 3.11?

I think a similar change in 3.11 would actually be simpler: just check if an exception table exists. If not, don't throw.

The only reason we need the YIELD_VALUE oparg in this 3.12 patch is because every generator now has an implicit try/except around the entire body, so it's a bit trickier to figure out if we're in that handler (which we can safely ignore) or something else.

@gvanrossum
Copy link
Member

Great, it'll just be different. :-)

@markshannon markshannon added the needs backport to 3.11 only security fixes label Jan 24, 2023
@markshannon markshannon merged commit f02fa64 into python:main Jan 24, 2023
@miss-islington
Copy link
Contributor

Thanks @markshannon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @markshannon, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker f02fa64bf2d03ef7a28650c164e17a5fb5d8543d 3.11

@brandtbucher
Copy link
Member

I don’t think this should be backported as-is, right? (See my comment above.)

@markshannon
Copy link
Member Author

markshannon commented Jan 25, 2023

@brandtbucher agreed.
The changes to genobject.c need backporting (with the change you suggest for determining when to skip the call to gen.throw(), but the other changes are 3.12 specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs backport to 3.11 only security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants