Skip to content

Commit 85016b5

Browse files
authored
Merge pull request swiftlang#68255 from btroller/btroller-rdar111248508-cache-additional-TypeRefBuilder-operations
[RemoteMirror] Speed up Remote Mirror significantly by caching two additional calculations in TypeRefBuilder.
2 parents 118fd86 + f93f7ad commit 85016b5

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

include/swift/RemoteInspection/TypeRefBuilder.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,18 @@ class TypeRefBuilder {
424424
/// Cache for field info lookups.
425425
std::unordered_map<std::string, RemoteRef<FieldDescriptor>> FieldTypeInfoCache;
426426

427+
/// Cache for normalized reflection name lookups.
428+
std::unordered_map<uint64_t /* remote address */, llvm::Optional<std::string>>
429+
NormalizedReflectionNameCache;
430+
431+
/// Cache for built-in type descriptor lookups.
432+
std::unordered_map<std::string /* normalized name */,
433+
RemoteRef<BuiltinTypeDescriptor>>
434+
BuiltInTypeDescriptorCache;
435+
436+
/// The index of the last ReflectionInfo cached by BuiltInTypeDescriptorCache.
437+
uint32_t NormalizedReflectionNameCacheLastReflectionInfoCache = 0;
438+
427439
std::vector<std::unique_ptr<const GenericSignatureRef>> SignatureRefPool;
428440

429441
TypeConverter TC;

stdlib/public/RemoteInspection/TypeRefBuilder.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ RemoteRef<char> TypeRefBuilder::readTypeRef(uint64_t remoteAddr) {
9393
/// Load and normalize a mangled name so it can be matched with string equality.
9494
llvm::Optional<std::string>
9595
TypeRefBuilder::normalizeReflectionName(RemoteRef<char> reflectionName) {
96+
const auto reflectionNameRemoteAddress = reflectionName.getAddressData();
97+
98+
if (const auto found =
99+
NormalizedReflectionNameCache.find(reflectionNameRemoteAddress);
100+
found != NormalizedReflectionNameCache.end()) {
101+
return found->second;
102+
}
103+
96104
ScopedNodeFactoryCheckpoint checkpoint(this);
97105
// Remangle the reflection name to resolve symbolic references.
98106
if (auto node = demangleTypeRef(reflectionName,
@@ -102,18 +110,27 @@ TypeRefBuilder::normalizeReflectionName(RemoteRef<char> reflectionName) {
102110
case Node::Kind::ProtocolSymbolicReference:
103111
case Node::Kind::OpaqueTypeDescriptorSymbolicReference:
104112
// Symbolic references cannot be mangled, return a failure.
113+
NormalizedReflectionNameCache.insert(std::make_pair(
114+
reflectionNameRemoteAddress, llvm::Optional<std::string>()));
105115
return {};
106116
default:
107117
auto mangling = mangleNode(node);
108118
if (!mangling.isSuccess()) {
119+
NormalizedReflectionNameCache.insert(std::make_pair(
120+
reflectionNameRemoteAddress, llvm::Optional<std::string>()));
109121
return {};
110122
}
123+
NormalizedReflectionNameCache.insert(
124+
std::make_pair(reflectionNameRemoteAddress, mangling.result()));
111125
return std::move(mangling.result());
112126
}
113127
}
114128

115129
// Fall back to the raw string.
116-
return getTypeRefString(reflectionName).str();
130+
const auto manglingResult = getTypeRefString(reflectionName).str();
131+
NormalizedReflectionNameCache.insert(
132+
std::make_pair(reflectionNameRemoteAddress, manglingResult));
133+
return std::move(manglingResult);
117134
}
118135

119136
/// Determine whether the given reflection protocol name matches.
@@ -398,8 +415,12 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
398415
else
399416
return nullptr;
400417

401-
for (auto Info : ReflectionInfos) {
402-
for (auto BuiltinTypeDescriptor : Info.Builtin) {
418+
for (; NormalizedReflectionNameCacheLastReflectionInfoCache <
419+
ReflectionInfos.size();
420+
NormalizedReflectionNameCacheLastReflectionInfoCache++) {
421+
for (auto BuiltinTypeDescriptor :
422+
ReflectionInfos[NormalizedReflectionNameCacheLastReflectionInfoCache]
423+
.Builtin) {
403424
if (BuiltinTypeDescriptor->Stride <= 0)
404425
continue;
405426
if (!BuiltinTypeDescriptor->hasMangledTypeName())
@@ -413,13 +434,21 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
413434
continue;
414435

415436
auto CandidateMangledName =
416-
readTypeRef(BuiltinTypeDescriptor, BuiltinTypeDescriptor->TypeName);
417-
if (!reflectionNameMatches(CandidateMangledName, MangledName))
418-
continue;
419-
return BuiltinTypeDescriptor;
437+
readTypeRef(BuiltinTypeDescriptor, BuiltinTypeDescriptor->TypeName);
438+
auto CandidateNormalizedName =
439+
normalizeReflectionName(CandidateMangledName);
440+
if (CandidateNormalizedName) {
441+
BuiltInTypeDescriptorCache.insert(
442+
std::make_pair(*CandidateNormalizedName, BuiltinTypeDescriptor));
443+
}
420444
}
421445
}
422446

447+
if (const auto found = BuiltInTypeDescriptorCache.find(MangledName);
448+
found != BuiltInTypeDescriptorCache.end()) {
449+
return found->second;
450+
}
451+
423452
return nullptr;
424453
}
425454

0 commit comments

Comments
 (0)