← 返回首页
gh-74690: Micro-optimise `typing._get_protocol_attrs` by AlexWaygood · Pull Request #103152 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

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

Provide feedback

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

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-74690: Micro-optimise typing._get_protocol_attrs#103152

Merged
AlexWaygood merged 2 commits into
python:mainfrom
AlexWaygood:micro-optimise-protocols
Mar 31, 2023
Merged

gh-74690: Micro-optimise typing._get_protocol_attrs#103152
AlexWaygood merged 2 commits into
python:mainfrom
AlexWaygood:micro-optimise-protocols

Conversation

AlexWaygood commented Mar 31, 2023
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

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:
import time from typing import Protocol, runtime_checkable @runtime_checkable class HasX(Protocol): x: int class Foo: @property def x(self) -> int: return 42 class Bar: x = 42 class Baz: def __init__(self): self.x = 42 class Egg: ... class Nominal(HasX): def __init__(self): self.x = 42 class Registered: ... HasX.register(Registered) num_instances = 500_000 foos = [Foo() for _ in range(num_instances)] bars = [Bar() for _ in range(num_instances)] bazzes = [Baz() for _ in range(num_instances)] basket = [Egg() for _ in range(num_instances)] nominals = [Nominal() for _ in range(num_instances)] registereds = [Registered() for _ in range(num_instances)] def bench(objs, title): start_time = time.perf_counter() for obj in objs: isinstance(obj, HasX) elapsed = time.perf_counter() - start_time print(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.

AlexWaygood 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
AlexWaygood requested a review from carljm March 31, 2023 18:36

Copy link
Copy Markdown
Member Author

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

carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

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?

Comment thread Lib/typing.py Outdated Show resolved Hide resolved

hauntsaninja left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Hooray, thanks for working on this!

Copy link
Copy Markdown
Member Author

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 👍

hauntsaninja left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Looks great!

AlexWaygood merged commit 361a3ea into python:main Mar 31, 2023
AlexWaygood 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.12 only security fixes performance Performance or resource usage skip news stdlib Standard Library Python modules in the Lib/ directory topic-typing type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Footer

© 2026 GitHub, Inc.