diff --git a/include/swift/RemoteInspection/TypeRefBuilder.h b/include/swift/RemoteInspection/TypeRefBuilder.h index cd562ec8d0b2e..c9f93fca1ad3f 100644 --- a/include/swift/RemoteInspection/TypeRefBuilder.h +++ b/include/swift/RemoteInspection/TypeRefBuilder.h @@ -424,6 +424,18 @@ class TypeRefBuilder { /// Cache for field info lookups. std::unordered_map> FieldTypeInfoCache; + /// Cache for normalized reflection name lookups. + std::unordered_map> + NormalizedReflectionNameCache; + + /// Cache for built-in type descriptor lookups. + std::unordered_map> + BuiltInTypeDescriptorCache; + + /// The index of the last ReflectionInfo cached by BuiltInTypeDescriptorCache. + uint32_t NormalizedReflectionNameCacheLastReflectionInfoCache = 0; + std::vector> SignatureRefPool; TypeConverter TC; diff --git a/stdlib/public/RemoteInspection/TypeRefBuilder.cpp b/stdlib/public/RemoteInspection/TypeRefBuilder.cpp index 77486c36bd9fe..fe36991d86f2c 100644 --- a/stdlib/public/RemoteInspection/TypeRefBuilder.cpp +++ b/stdlib/public/RemoteInspection/TypeRefBuilder.cpp @@ -93,6 +93,14 @@ RemoteRef TypeRefBuilder::readTypeRef(uint64_t remoteAddr) { /// Load and normalize a mangled name so it can be matched with string equality. llvm::Optional TypeRefBuilder::normalizeReflectionName(RemoteRef reflectionName) { + const auto reflectionNameRemoteAddress = reflectionName.getAddressData(); + + if (const auto found = + NormalizedReflectionNameCache.find(reflectionNameRemoteAddress); + found != NormalizedReflectionNameCache.end()) { + return found->second; + } + ScopedNodeFactoryCheckpoint checkpoint(this); // Remangle the reflection name to resolve symbolic references. if (auto node = demangleTypeRef(reflectionName, @@ -102,18 +110,27 @@ TypeRefBuilder::normalizeReflectionName(RemoteRef reflectionName) { case Node::Kind::ProtocolSymbolicReference: case Node::Kind::OpaqueTypeDescriptorSymbolicReference: // Symbolic references cannot be mangled, return a failure. + NormalizedReflectionNameCache.insert(std::make_pair( + reflectionNameRemoteAddress, llvm::Optional())); return {}; default: auto mangling = mangleNode(node); if (!mangling.isSuccess()) { + NormalizedReflectionNameCache.insert(std::make_pair( + reflectionNameRemoteAddress, llvm::Optional())); return {}; } + NormalizedReflectionNameCache.insert( + std::make_pair(reflectionNameRemoteAddress, mangling.result())); return std::move(mangling.result()); } } // Fall back to the raw string. - return getTypeRefString(reflectionName).str(); + const auto manglingResult = getTypeRefString(reflectionName).str(); + NormalizedReflectionNameCache.insert( + std::make_pair(reflectionNameRemoteAddress, manglingResult)); + return std::move(manglingResult); } /// Determine whether the given reflection protocol name matches. @@ -398,8 +415,12 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) { else return nullptr; - for (auto Info : ReflectionInfos) { - for (auto BuiltinTypeDescriptor : Info.Builtin) { + for (; NormalizedReflectionNameCacheLastReflectionInfoCache < + ReflectionInfos.size(); + NormalizedReflectionNameCacheLastReflectionInfoCache++) { + for (auto BuiltinTypeDescriptor : + ReflectionInfos[NormalizedReflectionNameCacheLastReflectionInfoCache] + .Builtin) { if (BuiltinTypeDescriptor->Stride <= 0) continue; if (!BuiltinTypeDescriptor->hasMangledTypeName()) @@ -413,13 +434,21 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) { continue; auto CandidateMangledName = - readTypeRef(BuiltinTypeDescriptor, BuiltinTypeDescriptor->TypeName); - if (!reflectionNameMatches(CandidateMangledName, MangledName)) - continue; - return BuiltinTypeDescriptor; + readTypeRef(BuiltinTypeDescriptor, BuiltinTypeDescriptor->TypeName); + auto CandidateNormalizedName = + normalizeReflectionName(CandidateMangledName); + if (CandidateNormalizedName) { + BuiltInTypeDescriptorCache.insert( + std::make_pair(*CandidateNormalizedName, BuiltinTypeDescriptor)); + } } } + if (const auto found = BuiltInTypeDescriptorCache.find(MangledName); + found != BuiltInTypeDescriptorCache.end()) { + return found->second; + } + return nullptr; }