Skip to content

[RemoteMirrors] Put a cache in front of getFieldTypeInfo to avoid repetitive scanning of the target's reflection infos. #16996

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
merged 1 commit into from
Jun 5, 2018
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
5 changes: 5 additions & 0 deletions include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ class TypeRefBuilder {
std::unordered_map<TypeRefID, const TypeRef *,
TypeRefID::Hash, TypeRefID::Equal> AssociatedTypeCache;

/// Cache for field info lookups.
std::unordered_map<std::string,
std::pair<const FieldDescriptor *, const ReflectionInfo *>>
FieldTypeInfoCache;

TypeConverter TC;
MetadataSourceBuilder MSB;

Expand Down
39 changes: 27 additions & 12 deletions stdlib/public/Reflection/TypeRefBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,25 @@ TypeRefBuilder::getRemoteAddrOfTypeRefPointer(const void *pointer) {

TypeRefBuilder::TypeRefBuilder() : TC(*this) {}

/// Determine whether the given reflection protocol name matches.
static bool reflectionNameMatches(Demangler &dem,
StringRef reflectionName,
StringRef searchName) {
/// Normalize a mangled name so it can be matched with string equality.
static std::string normalizeReflectionName(Demangler &dem, StringRef reflectionName) {
reflectionName = dropSwiftManglingPrefix(reflectionName);

// Remangle the reflection name to resolve symbolic references.
if (auto node = dem.demangleType(reflectionName)) {
auto remangled = mangleNode(node);
return remangled == searchName;
return mangleNode(node);
}

// Fall back to string matching.
return reflectionName.equals(searchName);

// Fall back to the raw string.
return reflectionName;
}

/// Determine whether the given reflection protocol name matches.
static bool reflectionNameMatches(Demangler &dem,
StringRef reflectionName,
StringRef searchName) {
auto normalized = normalizeReflectionName(dem, reflectionName);
return searchName.equals(normalized);
}

const TypeRef * TypeRefBuilder::
Expand Down Expand Up @@ -139,6 +144,12 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
else
return {};

// Try the cache.
auto Found = FieldTypeInfoCache.find(MangledName);
if (Found != FieldTypeInfoCache.end())
return Found->second;

// On failure, fill out the cache with everything we know about.
std::vector<std::pair<std::string, const TypeRef *>> Fields;
for (auto &Info : ReflectionInfos) {
uintptr_t TypeRefOffset = Info.Field.SectionOffset
Expand All @@ -147,12 +158,16 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
if (!FD.hasMangledTypeName())
continue;
auto CandidateMangledName = FD.getMangledTypeName(TypeRefOffset);
if (!reflectionNameMatches(Dem, CandidateMangledName, MangledName))
continue;
return {&FD, &Info};
auto NormalizedName = normalizeReflectionName(Dem, CandidateMangledName);
FieldTypeInfoCache[NormalizedName] = {&FD, &Info};
}
}

// We've filled the cache with everything we know about now. Try the cache again.
Found = FieldTypeInfoCache.find(MangledName);
if (Found != FieldTypeInfoCache.end())
return Found->second;

return {nullptr, 0};
}

Expand Down