Skip to content

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

@eendebakpt

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

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