Skip to content

Speed up inspect.getattr_static for the common-metaclass case #149436

Closed

Description

@eendebakpt

Feature or enhancement

Proposal:

inspect._check_class walks an MRO and calls _shadowed_dict(type(entry))
per entry. Performance can be improved by:

  • Adding a fast path for the common case where type(entry) is type. type.__dict__ is never shadowed, so the answer is unconditionally _sentinel.
  • A large percentage of consecutive MRO entries share their metaclass.

Pyperformance results on typing_runtime_protocols:

main                          139 ± 2 µs
+ type shortcut               111 ± 1 µs    1.25x faster
+ last-metaclass cache        104 ± 1 µs    1.34x faster

Continues the optimization work in gh-103193 / gh-74690.

@AlexWaygood

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    performancePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directory
    on May 5, 2026
  2. added a commit that references this issue on May 10, 2026
  3. JelleZijlstra commented on May 10, 2026

    @JelleZijlstra
    Member

    Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions