Skip to content

Commit 516c21a

Browse files
committed
[Runtime] Fast-path lookup of protocol descriptors with standard manglings.
We weren't taking advantage of standard manglings in _findProtocolDescriptor like wo do in _findContextDescriptor. Extract out the "standard mangling" code and call it from both to speed up those searches. rdar://111235115
1 parent bd8e6bc commit 516c21a

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -785,19 +785,11 @@ _searchTypeMetadataRecords(TypeMetadataPrivateState &T,
785785

786786
static const ConcurrencyStandardTypeDescriptors *concurrencyDescriptors;
787787

788+
/// Perform a fast-path lookup for standard library type references with short
789+
/// manglings. Returns the appropriate descriptor, or NULL if the descriptor
790+
/// couldn't be resolved, or if the node does not refer to one of those types.
788791
static const ContextDescriptor *
789-
_findContextDescriptor(Demangle::NodePointer node,
790-
Demangle::Demangler &Dem) {
791-
NodePointer symbolicNode = node;
792-
if (symbolicNode->getKind() == Node::Kind::Type)
793-
symbolicNode = symbolicNode->getChild(0);
794-
795-
// If we have a symbolic reference to a context, resolve it immediately.
796-
if (symbolicNode->getKind() == Node::Kind::TypeSymbolicReference) {
797-
return cast<TypeContextDescriptor>(
798-
(const ContextDescriptor *)symbolicNode->getIndex());
799-
}
800-
792+
descriptorFromStandardMangling(Demangle::NodePointer symbolicNode) {
801793
#if SWIFT_STDLIB_SHORT_MANGLING_LOOKUPS
802794
// Fast-path lookup for standard library type references with short manglings.
803795
if (symbolicNode->getNumChildren() >= 2
@@ -823,6 +815,24 @@ _findContextDescriptor(Demangle::NodePointer node,
823815
#include "swift/Demangling/StandardTypesMangling.def"
824816
}
825817
#endif
818+
return nullptr;
819+
}
820+
821+
static const ContextDescriptor *
822+
_findContextDescriptor(Demangle::NodePointer node,
823+
Demangle::Demangler &Dem) {
824+
NodePointer symbolicNode = node;
825+
if (symbolicNode->getKind() == Node::Kind::Type)
826+
symbolicNode = symbolicNode->getChild(0);
827+
828+
// If we have a symbolic reference to a context, resolve it immediately.
829+
if (symbolicNode->getKind() == Node::Kind::TypeSymbolicReference) {
830+
return cast<TypeContextDescriptor>(
831+
(const ContextDescriptor *)symbolicNode->getIndex());
832+
}
833+
834+
if (auto *standardDescriptor = descriptorFromStandardMangling(symbolicNode))
835+
return standardDescriptor;
826836

827837
const ContextDescriptor *foundContext = nullptr;
828838
auto &T = TypeMetadataRecords.get();
@@ -989,8 +999,7 @@ _searchProtocolRecords(ProtocolMetadataPrivateState &C,
989999

9901000
static const ProtocolDescriptor *
9911001
_findProtocolDescriptor(NodePointer node,
992-
Demangle::Demangler &Dem,
993-
std::string &mangledName) {
1002+
Demangle::Demangler &Dem) {
9941003
const ProtocolDescriptor *foundProtocol = nullptr;
9951004
auto &T = Protocols.get();
9961005

@@ -1002,13 +1011,18 @@ _findProtocolDescriptor(NodePointer node,
10021011
return cast<ProtocolDescriptor>(
10031012
(const ContextDescriptor *)symbolicNode->getIndex());
10041013

1014+
if (auto *standardDescriptor = descriptorFromStandardMangling(symbolicNode)) {
1015+
assert(standardDescriptor->getKind() == ContextDescriptorKind::Protocol);
1016+
return static_cast<const ProtocolDescriptor *>(standardDescriptor);
1017+
}
1018+
10051019
auto mangling =
10061020
Demangle::mangleNode(node, ExpandResolvedSymbolicReferences(Dem), Dem);
10071021

10081022
if (!mangling.isSuccess())
10091023
return nullptr;
10101024

1011-
mangledName = mangling.result().str();
1025+
auto mangledName = mangling.result().str();
10121026

10131027
// Look for an existing entry.
10141028
// Find the bucket for the metadata entry.
@@ -1622,8 +1636,7 @@ class DecodedMetadataBuilder {
16221636

16231637
BuiltProtocolDecl createProtocolDecl(NodePointer node) const {
16241638
// Look for a protocol descriptor based on its mangled name.
1625-
std::string mangledName;
1626-
if (auto protocol = _findProtocolDescriptor(node, demangler, mangledName))
1639+
if (auto protocol = _findProtocolDescriptor(node, demangler))
16271640
return ProtocolDescriptorRef::forSwift(protocol);;
16281641

16291642
#if SWIFT_OBJC_INTEROP

0 commit comments

Comments
 (0)