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-74690: Micro-optimisetyping._get_protocol_attrs#103152

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

Merged

Conversation

AlexWaygood
Copy link
Member

@AlexWaygoodAlexWaygood commentedMar 31, 2023
edited by bedevere-bot
Loading

This PR is a bunch of micro-optimisations fortyping._get_protocol_attrs that together have a significant impact on the speed ofisinstance checks against runtime-checkable protocols.

Benchmark:
importtimefromtypingimportProtocol,runtime_checkable@runtime_checkableclassHasX(Protocol):x:intclassFoo:@propertydefx(self)->int:return42classBar:x=42classBaz:def__init__(self):self.x=42classEgg: ...classNominal(HasX):def__init__(self):self.x=42classRegistered: ...HasX.register(Registered)num_instances=500_000foos= [Foo()for_inrange(num_instances)]bars= [Bar()for_inrange(num_instances)]bazzes= [Baz()for_inrange(num_instances)]basket= [Egg()for_inrange(num_instances)]nominals= [Nominal()for_inrange(num_instances)]registereds= [Registered()for_inrange(num_instances)]defbench(objs,title):start_time=time.perf_counter()forobjinobjs:isinstance(obj,HasX)elapsed=time.perf_counter()-start_timeprint(f"{title}:{elapsed:.2f}")bench(foos,"Time taken for objects with a property")bench(bars,"Time taken for objects with a classvar")bench(bazzes,"Time taken for objects with an instance var")bench(basket,"Time taken for objects with no var")bench(nominals,"Time taken for nominal subclass instances")bench(registereds,"Time taken for registered subclass instances")

Results ondfc4c95:

Time taken for objects with a property: 2.17Time taken for objects with a classvar: 2.10Time taken for objects with an instance var: 2.13Time taken for objects with no var: 2.47Time taken for nominal subclass instances: 2.20Time taken for registered subclass instances: 4.72

Results with this PR:

Time taken for objects with a property: 1.65Time taken for objects with a classvar: 1.60Time taken for objects with an instance var: 1.61Time taken for objects with no var: 1.89Time taken for nominal subclass instances: 1.61Time taken for registered subclass instances: 3.79

I'm once again skipping NEWS, since#103034 will counteract the boost to performance here, so the overall impact on performance for runtime-checkable protocols in 3.12 is still TBD.

@AlexWaygood
Copy link
MemberAuthor

May once again be of interest to@leycec and@posita :)

posita reacted with hooray emojiposita reacted with heart emojiposita reacted with eyes emoji

Copy link
Member

@carljmcarljm left a comment

Choose a reason for hiding this comment

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

Looks fine to me.

Given@hauntsaninja 's feedback in#103141 , are you planning to land a single news entry for this and that one and#103034 that summarizes the changes and the overall perf impact?

AlexWaygood reacted with rocket emoji
Copy link
Contributor

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Hooray, thanks for working on this!

AlexWaygood reacted with heart emoji
@AlexWaygood
Copy link
MemberAuthor

Given@hauntsaninja 's feedback in#103141 , are you planning to land a single news entry for this and that one and#103034 that summarizes the changes and the overall perf impact?

Yeah, I'll try to rework the NEWS entry in#103034 to summarise the changes made in the performance-related PRs as well as that PR 👍

carljm reacted with thumbs up emoji

Copy link
Contributor

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Looks great!

AlexWaygood reacted with heart emoji
@AlexWaygoodAlexWaygood merged commit361a3ea intopython:mainMar 31, 2023
@AlexWaygoodAlexWaygood deleted the micro-optimise-protocols branchMarch 31, 2023 20:54
warsaw pushed a commit to warsaw/cpython that referenced this pull requestApr 11, 2023
…03152)Improve performance of `isinstance()` checks against runtime-checkable protocols
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@carljmcarljmcarljm approved these changes

@hauntsaninjahauntsaninjahauntsaninja approved these changes

@gvanrossumgvanrossumAwaiting requested review from gvanrossum

@Fidget-SpinnerFidget-SpinnerAwaiting requested review from Fidget-Spinner

@JelleZijlstraJelleZijlstraAwaiting requested review from JelleZijlstraJelleZijlstra is a code owner

Assignees
No one assigned
Labels
3.12only security fixesperformancePerformance or resource usageskip newsstdlibPython modules in the Lib dirtopic-typingtype-featureA feature request or enhancement
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@AlexWaygood@carljm@hauntsaninja@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp