GeezerVison is showing github.com — gh-118013: Use weakrefs for the cache key in `inspect._shadowed_dict` by AlexWaygood · Pull Request #118202 · python/cpython
Conversation
AlexWaygood
commented
Apr 23, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Slower than main, but significantly faster than a straight revert of #104267.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, once we get news/tests/benchmarks.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Benchmarks
All timings done with a fresh PGO-optimised build on main using a MacBook pro.
getattr_static benchmarks:
Benchmark script
from timeit import timeit
from statistics import mean, stdev
from inspect import getattr_static
class Foo:
@property
def x(self) -> int:
return 42
class Bar:
x = 42
class WithParentClassX(Bar): ...
class Baz:
def __init__(self):
self.x = 42
class WithParentX(Baz): ...
class Missing: ...
class Slotted:
__slots__ = ('x',)
def __init__(self):
self.x = 42
class Method:
def x(self): ...
class ClsMethod:
@classmethod
def x(cls): ...
class StMethod:
@staticmethod
def x(): ...
import gc
gc.disable()
times = []
def stats():
ts = [t * 1e8 for t in sorted(times)[:5]]
return f'{round(mean(ts)):4} ± {round(stdev(ts)):2} ns '
def bench(obj):
# Warmup:
for _ in range(5):
number = 100
timeit(lambda: getattr_static(obj, 'x', None), number=number)
# Actual bench:
for _ in range(50):
number = 1000
t = timeit(lambda: getattr_static(obj, 'x', None), number=number) / number
times.append(t)
bench_name = (
f'type[{obj.__name__}]'
if isinstance(obj, type)
else obj.__class__.__name__
)
print(f"{bench_name: <25}: {stats()}")
times.clear()
bench(Foo)
bench(Foo())
bench(Bar)
bench(Bar())
bench(WithParentClassX())
bench(Baz())
bench(WithParentX())
bench(Missing)
bench(Missing())
bench(Slotted())
bench(Method())
bench(StMethod())
bench(ClsMethod())
Results on main
type[Foo] : 26 ± 1 ns
Foo : 44 ± 0 ns
type[Bar] : 26 ± 0 ns
Bar : 40 ± 0 ns
WithParentClassX : 52 ± 0 ns
Baz : 49 ± 0 ns
WithParentX : 59 ± 0 ns
type[Missing] : 56 ± 0 ns
Missing : 50 ± 0 ns
Slotted : 57 ± 0 ns
Method : 39 ± 0 ns
StMethod : 39 ± 0 ns
ClsMethod : 39 ± 0 ns
Results with this PR
type[Foo] : 39 ± 0 ns
Foo : 60 ± 1 ns
type[Bar] : 33 ± 0 ns
Bar : 56 ± 0 ns
WithParentClassX : 79 ± 0 ns
Baz : 72 ± 0 ns
WithParentX : 92 ± 0 ns
type[Missing] : 86 ± 0 ns
Missing : 74 ± 0 ns
Slotted : 74 ± 0 ns
Method : 56 ± 0 ns
StMethod : 56 ± 0 ns
ClsMethod : 56 ± 0 ns
Results with a clean revert of #104267
type[Foo] : 43 ± 0 ns
Foo : 66 ± 1 ns
type[Bar] : 37 ± 0 ns
Bar : 65 ± 0 ns
WithParentClassX : 91 ± 0 ns
Baz : 84 ± 0 ns
WithParentX : 111 ± 0 ns
type[Missing] : 103 ± 0 ns
Missing : 85 ± 0 ns
Slotted : 78 ± 0 ns
Method : 65 ± 0 ns
StMethod : 65 ± 0 ns
ClsMethod : 65 ± 0 ns
Runtime-checkable protocol benchmarks
Benchmark script
from typing import Protocol, runtime_checkable
@runtime_checkable
class Foo(Protocol):
a: int
b: int
c: int
d: int
e: int
f: int
g: int
h: int
i: int
j: int
k: int
l: int
m: int
n: int
o: int
p: int
q: int
r: int
s: int
t: int
u: int
v: int
w: int
x: int
y: int
z: int
class Bar:
def __init__(self):
for attrname in 'abcdefghijklmnopqrstuvwxyz':
setattr(self, attrname, 42)
bars = [Bar() for _ in range(100_000)]
import time
start = time.perf_counter()
for bar in bars:
isinstance(bar, Foo)
print(time.perf_counter() - start)
Results on main: 1.3446190829854459
Results with this PR: 1.9680303339846432
Results with a clean revert of #104267: 2.243281374918297
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AlexWaygood reacted with rocket emoji
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
carljm
carljm approved these changes
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.
|