GeezerVison is showing github.com — gh-103193: Speedup and inline `inspect._is_type` by AlexWaygood · Pull Request #103321 · python/cpython
Conversation
AlexWaygood
commented
Apr 6, 2023
•
edited by bedevere-bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
(Using @sobolevn's benchmark script from #103193 (comment).)
Benchmarks on main:
type[Foo] : 74 ± 0 ns
Foo : 177 ± 1 ns
type[Bar] : 74 ± 0 ns
Bar : 177 ± 0 ns
WithParentClassX : 229 ± 0 ns
Baz : 216 ± 0 ns
WithParentX : 266 ± 1 ns
type[Missing] : 210 ± 0 ns
Missing : 219 ± 1 ns
Slotted : 214 ± 0 ns
Method : 178 ± 0 ns
StMethod : 178 ± 0 ns
ClsMethod : 178 ± 1 ns
Benchmarks with this PR:
type[Foo] : 74 ± 0 ns
Foo : 134 ± 0 ns
type[Bar] : 74 ± 0 ns
Bar : 133 ± 0 ns
WithParentClassX : 187 ± 0 ns
Baz : 171 ± 0 ns
WithParentX : 220 ± 0 ns
type[Missing] : 210 ± 1 ns
Missing : 174 ± 0 ns
Slotted : 169 ± 0 ns
Method : 133 ± 0 ns
StMethod : 133 ± 0 ns
ClsMethod : 133 ± 0 ns
This speeds up this isinstance() call 1.25x relative to main:
from typing import *
@runtime_checkable
class Foo(Protocol):
a: int
b: int
c: int
d: int
e: int
f: int
g: int
h: int
i: int
j: int
class Bar:
def __init__(self):
for attrname in 'abcdefghij':
setattr(self, attrname, 42)
isinstance(Bar(), Foo)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The improvements keep on coming!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+1818
to
+1820
|
|
objtype = type(obj)
|
|
|
if type not in _static_getmro(objtype):
|
|
|
klass = objtype
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has equivalent semantics with less code and fewer assignments, but perhaps it muddies the semantics of the klass var a bit (since it will temporarily be "wrong" in the case that obj is already a type.) No strong feelings, whatever you prefer.
|
objtype = type(obj) |
|
if type not in _static_getmro(objtype): |
|
klass = objtype |
|
klass = type(obj) |
|
if type not in _static_getmro(klass): |
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. Yeah, I see what you mean, but I think I'd prefer to keep the semantics of klass clear here :)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
carljm reacted with thumbs up emoji
The improvements keep on coming!
Optimisations will continue until morale improves!
|
carljm reacted with laugh emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Apr 6, 2023
warsaw
pushed a commit
to warsaw/cpython
that referenced
this pull request
Apr 11, 2023
Improve performance of `inspect.getattr_static`
This comment was marked as spam.
Reviewers
carljm
carljm approved these changes
sobolevn
Awaiting requested review from sobolevn
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
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.
|