Backport performance improvements to runtime-checkable protocols#137
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR backports the following CPython PRs (all by me), which, taken together, substantially improve the performance of
isinstance()checks against runtime-checkable protocols on Python 3.12 compared to Python 3.11:_get_protocol_attrstwice in_ProtocolMeta.__instancecheck__cpython#103141typing._get_protocol_attrscpython#103152_ProtocolMeta.__instancecheck__cpython#103159_get_protocol_attrsand_callable_members_onlyat protocol class creation time, not duringisinstance()checks cpython#103160typing._ProtocolMeta.__instancecheck__cpython#103280Here is a benchmark script:
Benchmark results on `main`, using Python 3.11
Benchmark results with this PR branch, using Python 3.11
Note that if we choose to backport python/cpython#103034 (using
inspect.getattr_staticinstead ofgetattrin_ProtocolMeta.__instancecheck__), it will undo a lot of the performance improvements that this PR brings. I leave the decision of whether or not to backport that PR to another PR/issue.