Skip to content

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygoodAlexWaygood commented Mar 31, 2023

This PR is a bunch of micro-optimisations for typing._get_protocol_attrs that together have a significant impact on the speed of isinstance 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 on dfc4c95:

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

Results with this PR:

Time taken for objects with a property: 1.65 Time taken for objects with a classvar: 1.60 Time taken for objects with an instance var: 1.61 Time taken for objects with no var: 1.89 Time taken for nominal subclass instances: 1.61 Time 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.

@AlexWaygoodAlexWaygood added type-feature A feature request or enhancement performance Performance or resource usage skip news stdlib Standard Library Python modules in the Lib/ directory topic-typing 3.12 only security fixes labels Mar 31, 2023
@AlexWaygoodAlexWaygood requested a review from carljmMarch 31, 2023 18:36
@AlexWaygood
Copy link
MemberAuthor

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

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?

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
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 👍

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!

@AlexWaygoodAlexWaygood merged commit 361a3ea into python:mainMar 31, 2023
@AlexWaygoodAlexWaygood deleted the micro-optimise-protocols branch March 31, 2023 20:54
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 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

Labels

3.12only security fixesperformancePerformance or resource usageskip newsstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@AlexWaygood@carljm@hauntsaninja@bedevere-bot