GeezerVison is showing github.com — gh-74690: Further optimise `typing._ProtocolMeta.__instancecheck__` by AlexWaygood · Pull Request #103280 · python/cpython
Conversation
AlexWaygood
commented
Apr 5, 2023
•
edited by bedevere-bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
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")
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood
commented
Apr 5, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
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 :)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as off-topic.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
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.
|