-
Notifications
You must be signed in to change notification settings - Fork 10.5k
IRGen: fast casting to final classes #41784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@swift-ci test |
@swift-ci benchmark |
Does |
As discussed offline, ObjectiveC classes are excluded from this optimization anyway |
1a3c5e4
to
fbc792d
Compare
@swift-ci test |
@swift-ci benchmark |
This looks like it should apply to |
@tbkka When this exact check fails, do we then also need to check that the source's type is not Trying to answer my question: as long as this code is only reached when the static type of the cast's source is a class, then the dynamic type cannot be @eeckstein this optimization only happens when the source's static type is a class right? |
|
class or class existential, but not AnyObject |
It turns out that casting an existential to a class may return a different object than the source of the cast. Once we fix
Not sure how much that will affect the performance results until we get a runtime fix: rdar://90269352 (Always unwrap AnyHashable during dynamic casting) |
@atrick and I looked at the current runtime casting implementation and this optimization is not currently safe if the source is any kind of existential (including |
I'm not sure about this. As far as I understand, this can only happen if the source is an AnyObject, but not another class existential.Also, isn't this only the case for Linux (non-objc interop runtime)? |
I need to do a little more verification, but studying the casting code, it looks like this is a problem for all class existentials on all platforms. |
When casting a class instance to a final class, we can directly compare the isa-pointer with address of the metadata. This avoids a call to `swift_dynamicCastClass`. It also avoids a call to the metadata accessor of the class (which calls `swift_getInitializedObjCClass`). For comparing the metadata pointers it's not required that the metadata is fully initialized.
fbc792d
to
860078a
Compare
@atrick I'm using the new |
@swift-ci test |
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.
LGTM
This seems to have caused a pretty big regression on Foundation on Windows (CC: @millenomi) https://ci-external.swift.org/job/oss-swift-windows-toolchain-x86_64-vs2019/447/console |
Thanks, I'll disable this optimization for Windows. |
…imization This is a regression from swiftlang#41784. Fixes rdar://problem/93822961.
…imization This is a regression from swiftlang#41784. Fixes rdar://problem/93822961.
…imization This is a regression from swiftlang#41784. Fixes rdar://problem/93822961.
…imization This is a regression from swiftlang#41784. Fixes rdar://problem/93822961.
When casting a class instance to a final class, we can directly compare the isa-pointer with address of the metadata.
This avoids a call to
swift_dynamicCastClass
.It also avoids a call to the metadata accessor of the class (which calls
swift_getInitializedObjCClass
).For comparing the metadata pointers it's not required that the metadata is fully initialized.
It results in a significant performance improvement for class-casting intensive code. I measured 30% speedup for the new stack promotion + escape analysis, where we do a lot of dynamic instruction casting (#39438).