GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-74690: Further optimise `typing._ProtocolMeta.__instancecheck__` by AlexWaygood · Pull Request #103280 · python/cpython

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Apr 5, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I probably should have spotted this when doing #103034! This provides some more speedup for isinstance() checks against runtime-checkable protocols. The improvement is small, but noticeable.

Benchmarks on main:

Time taken for objects with a property: 2.53
Time taken for objects with a classvar: 2.59
Time taken for objects with an instance var: 3.18
Time taken for objects with no var: 2.38
Time taken for nominal subclass instances: 0.19
Time taken for registered subclass instances: 0.46

Benchmarks with this PR:

Time taken for objects with a property: 2.49
Time taken for objects with a classvar: 2.49
Time taken for objects with an instance var: 3.06
Time taken for objects with no var: 2.36
Time taken for nominal subclass instances: 0.18
Time taken for registered subclass instances: 0.47
Benchmark script
import time
from typing import Protocol, runtime_checkable

@runtime_checkable
class HasX(Protocol):
    x: int
    def foo(self): ...

class Foo:
    @property
    def x(self) -> int:
        return 42
    def foo(self): ...

class Bar:
    x = 42
    def foo(self): ...

class Baz:
    def __init__(self):
        self.x = 42
    def foo(self): ...

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")
  • Issue: Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance #74690

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
pythongh-74690: Further optimise `typing._ProtocolMeta.__instancechec…
eee4b2c
…k__`
@AlexWaygood AlexWaygood added type-feature A feature request or enhancement performance Performance or resource usage stdlib Standard Library Python modules in the Lib/ directory topic-typing 3.12 only security fixes labels Apr 5, 2023
@AlexWaygood
AlexWaygood requested a review from carljm April 5, 2023 15:00
@AlexWaygood
AlexWaygood requested review from Fidget-Spinner, JelleZijlstra and gvanrossum as code owners April 5, 2023 15:00
@bedevere-bot bedevere-bot added the awaiting core review label Apr 5, 2023
@bedevere-bot bedevere-bot mentioned this pull request Apr 5, 2023
Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance #74690
Closed
@AlexWaygood AlexWaygood added the skip news label Apr 5, 2023
@AlexWaygood

AlexWaygood commented Apr 5, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Continuing to skip news for now until we've reached a decision on whether we want to consider more radical optimisations that would change behaviour (#74690 (comment)). Once I'm confident we've finished optimising this code, I'll add Whatsnew/Docs/NEWS entries for all of the optimisation PRs together :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

carljm
carljm approved these changes Apr 5, 2023
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Apr 5, 2023
@AlexWaygood
AlexWaygood merged commit de18267 into python:main Apr 5, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Apr 5, 2023
@AlexWaygood
AlexWaygood deleted the more-optimisation branch April 5, 2023 16:37
@bedevere-bot

This comment was marked as off-topic.

Sign in to view
@AlexWaygood AlexWaygood mentioned this pull request Apr 7, 2023
gh-74690: Document changes made to runtime-checkable protocols in 3.12 #103348
Merged
gaogaotiantian pushed a commit to gaogaotiantian/cpython that referenced this pull request Apr 8, 2023
@AlexWaygood @gaogaotiantian
pythongh-74690: Further optimise `typing._ProtocolMeta.__instancechec…
d515fb0
…k__` (python#103280)
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@AlexWaygood @warsaw
pythongh-74690: Further optimise `typing._ProtocolMeta.__instancechec…
d9edf14
…k__` (python#103280)
@AlexWaygood AlexWaygood mentioned this pull request Apr 12, 2023
Backport performance improvements to runtime-checkable protocols python/typing_extensions#137
Merged
@AlexWaygood AlexWaygood mentioned this pull request May 24, 2023
[Bug] typing_extensions.Protocol unsupported under typing_extensions ≥ 4.6.0 beartype/beartype#241
Closed
@AlexWaygood AlexWaygood mentioned this pull request Aug 9, 2024
Is the protocol caching still needed? beartype/numerary#20
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@carljm carljm carljm approved these changes
@gvanrossum gvanrossum Awaiting requested review from gvanrossum
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner
@JelleZijlstra JelleZijlstra Awaiting requested review from JelleZijlstra JelleZijlstra is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

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

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@AlexWaygood @bedevere-bot @carljm
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.