-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
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
type-feature
A feature request or enhancement
performance
Performance or resource usage
stdlib
Python modules in the Lib dir
3.12
bugs and security fixes
labels
Apr 2, 2023
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 <[email protected]>
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
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
May 8, 2023
) (cherry picked from commit 921185e) Co-authored-by: Alex Waygood <[email protected]>
AlexWaygood
added a commit
that referenced
this issue
May 8, 2023
…104290) gh-103193: Improve `getattr_static` test coverage (GH-104286) (cherry picked from commit 921185e) Co-authored-by: Alex Waygood <[email protected]>
jbower-fb
pushed a commit
to jbower-fb/cpython-jbowerfb
that referenced
this issue
May 8, 2023
…getattr_static` (python#104267) Co-authored-by: Carl Meyer <[email protected]>
jbower-fb
pushed a commit
to jbower-fb/cpython-jbowerfb
that referenced
this issue
May 8, 2023
carljm
added a commit
to carljm/cpython
that referenced
this issue
May 9, 2023
* main: (47 commits) pythongh-97696 Remove unnecessary check for eager_start kwarg (python#104188) pythonGH-104308: socket.getnameinfo should release the GIL (python#104307) pythongh-104310: Add importlib.util.allowing_all_extensions() (pythongh-104311) pythongh-99113: A Per-Interpreter GIL! (pythongh-104210) pythonGH-104284: Fix documentation gettext build (python#104296) pythongh-89550: Buffer GzipFile.write to reduce execution time by ~15% (python#101251) pythongh-104223: Fix issues with inheriting from buffer classes (python#104227) pythongh-99108: fix typo in Modules/Setup (python#104293) pythonGH-104145: Use fully-qualified cross reference types for the bisect module (python#104172) pythongh-103193: Improve `getattr_static` test coverage (python#104286) Trim trailing whitespace and test on CI (python#104275) pythongh-102500: Remove mention of bytes shorthand (python#104281) pythongh-97696: Improve and fix documentation for asyncio eager tasks (python#104256) pythongh-99108: Replace SHA3 implementation HACL* version (python#103597) pythongh-104273: Remove redundant len() calls in argparse function (python#104274) pythongh-64660: Don't hardcode Argument Clinic return converter result variable name (python#104200) pythongh-104265 Disallow instantiation of `_csv.Reader` and `_csv.Writer` (python#104266) pythonGH-102613: Improve performance of `pathlib.Path.rglob()` (pythonGH-104244) pythongh-103650: Fix perf maps address format (python#103651) pythonGH-89812: Churn `pathlib.Path` methods (pythonGH-104243) ...
AlexWaygood
added a commit
to AlexWaygood/cpython
that referenced
this issue
May 9, 2023
kumaraditya303
pushed a commit
that referenced
this issue
May 9, 2023
carljm
added a commit
to carljm/cpython
that referenced
this issue
May 9, 2023
* main: pythongh-104276: Make `_struct.unpack_iterator` type use type flag instead of custom constructor (python#104277) pythongh-97696: Move around and update the whatsnew entry for asyncio eager task factory (python#104298) pythongh-103193: Fix refleaks in `test_inspect` and `test_typing` (python#104320) require-pr-label.yml: Add missing "permissions:" (python#104309) pythongh-90656: Add platform triplets for 64-bit LoongArch (LA64) (python#30939) pythongh-104180: Read SOCKS proxies from macOS System Configuration (python#104181)
carljm
added a commit
to carljm/cpython
that referenced
this issue
May 9, 2023
* main: (29 commits) pythongh-104276: Make `_struct.unpack_iterator` type use type flag instead of custom constructor (python#104277) pythongh-97696: Move around and update the whatsnew entry for asyncio eager task factory (python#104298) pythongh-103193: Fix refleaks in `test_inspect` and `test_typing` (python#104320) require-pr-label.yml: Add missing "permissions:" (python#104309) pythongh-90656: Add platform triplets for 64-bit LoongArch (LA64) (python#30939) pythongh-104180: Read SOCKS proxies from macOS System Configuration (python#104181) pythongh-97696 Remove unnecessary check for eager_start kwarg (python#104188) pythonGH-104308: socket.getnameinfo should release the GIL (python#104307) pythongh-104310: Add importlib.util.allowing_all_extensions() (pythongh-104311) pythongh-99113: A Per-Interpreter GIL! (pythongh-104210) pythonGH-104284: Fix documentation gettext build (python#104296) pythongh-89550: Buffer GzipFile.write to reduce execution time by ~15% (python#101251) pythongh-104223: Fix issues with inheriting from buffer classes (python#104227) pythongh-99108: fix typo in Modules/Setup (python#104293) pythonGH-104145: Use fully-qualified cross reference types for the bisect module (python#104172) pythongh-103193: Improve `getattr_static` test coverage (python#104286) Trim trailing whitespace and test on CI (python#104275) pythongh-102500: Remove mention of bytes shorthand (python#104281) pythongh-97696: Improve and fix documentation for asyncio eager tasks (python#104256) pythongh-99108: Replace SHA3 implementation HACL* version (python#103597) ...
carljm
added a commit
to carljm/cpython
that referenced
this issue
May 9, 2023
* main: (156 commits) pythongh-97696 Add documentation for get_coro() behavior with eager tasks (python#104304) pythongh-97933: (PEP 709) inline list/dict/set comprehensions (python#101441) pythongh-99889: Fix directory traversal security flaw in uu.decode() (python#104096) pythongh-104184: fix building --with-pydebug --enable-pystats (python#104217) pythongh-104139: Add itms-services to uses_netloc urllib.parse. (python#104312) pythongh-104240: return code unit metadata from codegen (python#104300) pythongh-104276: Make `_struct.unpack_iterator` type use type flag instead of custom constructor (python#104277) pythongh-97696: Move around and update the whatsnew entry for asyncio eager task factory (python#104298) pythongh-103193: Fix refleaks in `test_inspect` and `test_typing` (python#104320) require-pr-label.yml: Add missing "permissions:" (python#104309) pythongh-90656: Add platform triplets for 64-bit LoongArch (LA64) (python#30939) pythongh-104180: Read SOCKS proxies from macOS System Configuration (python#104181) pythongh-97696 Remove unnecessary check for eager_start kwarg (python#104188) pythonGH-104308: socket.getnameinfo should release the GIL (python#104307) pythongh-104310: Add importlib.util.allowing_all_extensions() (pythongh-104311) pythongh-99113: A Per-Interpreter GIL! (pythongh-104210) pythonGH-104284: Fix documentation gettext build (python#104296) pythongh-89550: Buffer GzipFile.write to reduce execution time by ~15% (python#101251) pythongh-104223: Fix issues with inheriting from buffer classes (python#104227) pythongh-99108: fix typo in Modules/Setup (python#104293) ...
carljm
added a commit
to carljm/cpython
that referenced
this issue
May 9, 2023
* main: (35 commits) pythongh-97696 Add documentation for get_coro() behavior with eager tasks (python#104304) pythongh-97933: (PEP 709) inline list/dict/set comprehensions (python#101441) pythongh-99889: Fix directory traversal security flaw in uu.decode() (python#104096) pythongh-104184: fix building --with-pydebug --enable-pystats (python#104217) pythongh-104139: Add itms-services to uses_netloc urllib.parse. (python#104312) pythongh-104240: return code unit metadata from codegen (python#104300) pythongh-104276: Make `_struct.unpack_iterator` type use type flag instead of custom constructor (python#104277) pythongh-97696: Move around and update the whatsnew entry for asyncio eager task factory (python#104298) pythongh-103193: Fix refleaks in `test_inspect` and `test_typing` (python#104320) require-pr-label.yml: Add missing "permissions:" (python#104309) pythongh-90656: Add platform triplets for 64-bit LoongArch (LA64) (python#30939) pythongh-104180: Read SOCKS proxies from macOS System Configuration (python#104181) pythongh-97696 Remove unnecessary check for eager_start kwarg (python#104188) pythonGH-104308: socket.getnameinfo should release the GIL (python#104307) pythongh-104310: Add importlib.util.allowing_all_extensions() (pythongh-104311) pythongh-99113: A Per-Interpreter GIL! (pythongh-104210) pythonGH-104284: Fix documentation gettext build (python#104296) pythongh-89550: Buffer GzipFile.write to reduce execution time by ~15% (python#101251) pythongh-104223: Fix issues with inheriting from buffer classes (python#104227) pythongh-99108: fix typo in Modules/Setup (python#104293) ...
16 tasks
This comment was marked as spam.
This comment was marked as spam.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
3.12
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_static
in 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_static
is 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_static
for introspection without side effects.Linked PRs
inspect.getattr_static
#103195inspect.getattr_static
#103318inspect._is_type
#103321inspect.getattr_static
in 'What's New in Python 3.12' #103349inspect._shadowed_dict
ininspect.getattr_static
#104267getattr_static
test coverage #104286getattr_static
test coverage (GH-104286) #104290test_inspect
andtest_typing
#104320The text was updated successfully, but these errors were encountered: