eendebakpt · GitHub

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

Read the original on github.com ↗