From e3cf5fa9246c4b38cd5342ad6d60dd359d1e8cb6 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 5 May 2026 20:51:01 +0200 Subject: [PATCH 1/4] inspect: fast path _shadowed_dict for type --- Lib/inspect.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/inspect.py b/Lib/inspect.py index d3af61b26e280a..344933ce4b1b9f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1740,6 +1740,9 @@ def _shadowed_dict(klass): # destroyed, and the dynamically created classes happen to be the only # objects that hold strong references to other objects that take up a # significant amount of memory. + # Fast path: `type` is the dominant caller; result is always _sentinel. + if klass is type: + return _sentinel return _shadowed_dict_from_weakref_mro_tuple( *[make_weakref(entry) for entry in _static_getmro(klass)] ) From dbd9fcf4970db7acde5368b0ac7c4adadc73ef38 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Tue, 5 May 2026 21:18:30 +0200 Subject: [PATCH 2/4] inspect: cache last metaclass in _check_class MRO loop Consecutive MRO entries usually share their metaclass, so call _shadowed_dict at most once per distinct metaclass. --- Lib/inspect.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 344933ce4b1b9f..b2d105cffa20ec 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1706,9 +1706,13 @@ def _check_instance(obj, attr): def _check_class(klass, attr): + last_meta = None for entry in _static_getmro(klass): - if _shadowed_dict(type(entry)) is _sentinel and attr in entry.__dict__: - return entry.__dict__[attr] + meta = type(entry) + if meta is last_meta or _shadowed_dict(meta) is _sentinel: + last_meta = meta + if attr in entry.__dict__: + return entry.__dict__[attr] return _sentinel From c45e06f03d870056c3bb04d97ca5503089dc1d5e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 8 May 2026 14:24:25 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst diff --git a/Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst b/Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst new file mode 100644 index 00000000000000..4dfcec001cbe3b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst @@ -0,0 +1 @@ +Improve performance of :method:`inspect.getattr_static`. From 2ff4a2a9b1033e8bed712e081f93bafd370cc602 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 8 May 2026 18:27:24 +0200 Subject: [PATCH 4/4] Apply suggestion from @eendebakpt --- .../next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst b/Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst index 4dfcec001cbe3b..401cf044e1514f 100644 --- a/Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst +++ b/Misc/NEWS.d/next/Library/2026-05-08-14-24-16.gh-issue-149436.dEV02X.rst @@ -1 +1 @@ -Improve performance of :method:`inspect.getattr_static`. +Improve performance of :meth:`inspect.getattr_static`.