Skip to content

Cache additional TypeRefBuilder operations #68255

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/swift/RemoteInspection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,18 @@ class TypeRefBuilder {
/// Cache for field info lookups.
std::unordered_map<std::string, RemoteRef<FieldDescriptor>> FieldTypeInfoCache;

/// Cache for normalized reflection name lookups.
std::unordered_map<uint64_t /* remote address */, llvm::Optional<std::string>>
NormalizedReflectionNameCache;

/// Cache for built-in type descriptor lookups.
std::unordered_map<std::string /* normalized name */,
RemoteRef<BuiltinTypeDescriptor>>
BuiltInTypeDescriptorCache;

/// The index of the last ReflectionInfo cached by BuiltInTypeDescriptorCache.
uint32_t NormalizedReflectionNameCacheLastReflectionInfoCache = 0;

std::vector<std::unique_ptr<const GenericSignatureRef>> SignatureRefPool;

TypeConverter TC;
Expand Down
43 changes: 36 additions & 7 deletions stdlib/public/RemoteInspection/TypeRefBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ RemoteRef<char> TypeRefBuilder::readTypeRef(uint64_t remoteAddr) {
/// Load and normalize a mangled name so it can be matched with string equality.
llvm::Optional<std::string>
TypeRefBuilder::normalizeReflectionName(RemoteRef<char> 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,
Expand All @@ -102,18 +110,27 @@ TypeRefBuilder::normalizeReflectionName(RemoteRef<char> 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<std::string>()));
return {};
default:
auto mangling = mangleNode(node);
if (!mangling.isSuccess()) {
NormalizedReflectionNameCache.insert(std::make_pair(
reflectionNameRemoteAddress, llvm::Optional<std::string>()));
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.
Expand Down Expand Up @@ -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())
Expand All @@ -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;
}

Expand Down