Skip to content

Commit 08233af

Browse files
authored
Extend the existing memoization of NSString's Hashable conformance to cover AnyObjects that are actually NSStrings as well (#82440)
Fixes rdar://154123172 (Consider memoizing the lookup for NSString's conformance to Hashable)
1 parent 376ce05 commit 08233af

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

stdlib/public/runtime/DynamicCast.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,17 @@ struct ObjCBridgeMemo {
850850
destBridgeWitness, targetBridgedType);
851851
}
852852
};
853+
854+
static const HashableWitnessTable* tryMemoizeNSStringHashableConformance(const Metadata *cls) {
855+
auto nsString = getNSStringMetadata();
856+
do {
857+
if (cls == nsString) {
858+
return getNSStringHashableConformance();
859+
}
860+
cls = _swift_class_getSuperclass(cls);
861+
} while (cls != nullptr);
862+
return nullptr;
863+
}
853864
#endif
854865

855866
static DynamicCastResult
@@ -867,23 +878,16 @@ tryCastToAnyHashable(
867878
const HashableWitnessTable *hashableConformance = nullptr;
868879

869880
switch (srcType->getKind()) {
881+
case MetadataKind::Existential: {
882+
return DynamicCastResult::Failure;
883+
}
870884
case MetadataKind::ForeignClass: // CF -> String
871885
case MetadataKind::ObjCClassWrapper: { // Obj-C -> String
872886
#if SWIFT_OBJC_INTEROP
873-
auto cls = srcType;
874-
auto nsString = getNSStringMetadata();
875-
do {
876-
if (cls == nsString) {
877-
hashableConformance = getNSStringHashableConformance();
878-
break;
879-
}
880-
cls = _swift_class_getSuperclass(cls);
881-
} while (cls != nullptr);
882-
break;
883-
#else
887+
hashableConformance = tryMemoizeNSStringHashableConformance(srcType);
888+
#endif
884889
// If no Obj-C interop, just fall through to the general case.
885890
break;
886-
#endif
887891
}
888892
case MetadataKind::Optional: {
889893
// FIXME: https://github.com/apple/swift/issues/51550

0 commit comments

Comments
 (0)