New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimise inspect.getattr_static
#103193
Labels
3.12
new features, bugs and security fixes
performance
Performance or resource usage
stdlib
Python modules in the Lib dir
type-feature
A feature request or enhancement
Comments
AlexWaygood
added a commit
to AlexWaygood/cpython
that referenced
this issue
Apr 2, 2023
AlexWaygood
added a commit
that referenced
this issue
Apr 5, 2023
|
I can't see anything further that can be easily optimised here, but happy to reopen if somebody else can! |
|
I've measured my ideas, but the results are close to noise. Initial numbers: After all changes: Code: 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()) |
AlexWaygood
added a commit
to AlexWaygood/cpython
that referenced
this issue
Apr 6, 2023
AlexWaygood
added a commit
that referenced
this issue
Apr 6, 2023
AlexWaygood
added a commit
to AlexWaygood/cpython
that referenced
this issue
Apr 6, 2023
AlexWaygood
added a commit
that referenced
this issue
Apr 6, 2023
Improve performance of `inspect.getattr_static`
|
I think I'm once again out of ideas, at least for now. |
AlexWaygood
added a commit
to AlexWaygood/cpython
that referenced
this issue
Apr 7, 2023
…tr_static` in 'What's New in Python 3.12'
AlexWaygood
added a commit
that referenced
this issue
Apr 7, 2023
…tic` in 'What's New in Python 3.12' (#103349)
gaogaotiantian
pushed a commit
to gaogaotiantian/cpython
that referenced
this issue
Apr 8, 2023
warsaw
pushed a commit
to warsaw/cpython
that referenced
this issue
Apr 11, 2023
warsaw
pushed a commit
to warsaw/cpython
that referenced
this issue
Apr 11, 2023
warsaw
pushed a commit
to warsaw/cpython
that referenced
this issue
Apr 11, 2023
Improve performance of `inspect.getattr_static`
warsaw
pushed a commit
to warsaw/cpython
that referenced
this issue
Apr 11, 2023
…tr_static` in 'What's New in Python 3.12' (python#103349)
AlexWaygood
added a commit
to AlexWaygood/cpython
that referenced
this issue
May 7, 2023
AlexWaygood
added a commit
that referenced
this issue
May 7, 2023
…r_static` (#104267) Co-authored-by: Carl Meyer <carl@oddbird.net>
AlexWaygood
added a commit
to AlexWaygood/cpython
that referenced
this issue
May 8, 2023
This was referenced May 8, 2023
AlexWaygood
added a commit
that referenced
this issue
May 8, 2023
AlexWaygood
added a commit
that referenced
this issue
May 8, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
3.12
new features, bugs and security fixes
performance
Performance or resource usage
stdlib
Python modules in the Lib dir
type-feature
A feature request or enhancement
Feature or enhancement
Following 6d59c9e,
typing._ProtocolMeta.__instancecheck__usesinspect.getattr_staticin a tight loop, where it had previously usedhasattr. This improves semantics in several highly desirable ways, but causes a considerable slowdown for_ProtocolMeta.__instancecheck__, asinspect.getattr_staticis much slower thanhasattr.The performance hit to
_ProtocolMeta.__instancecheck__has already been mostly mitigated through severaltyping-specific optimisations that are tracked in this issue:However, it would be good to also see if we can improve the performance of
inspect.getattr_static. This will not only improve the performance ofisinstance()checks against classes subclassingtyping.Protocol. It will also improve the performance of all other tools that useinspect.getattr_staticfor introspection without side effects.Linked PRs
inspect.getattr_static#103195inspect.getattr_static#103318inspect._is_type#103321inspect.getattr_staticin 'What's New in Python 3.12' #103349inspect._shadowed_dictininspect.getattr_static#104267getattr_statictest coverage #104286getattr_statictest coverage (GH-104286) #104290The text was updated successfully, but these errors were encountered: