@@ -50,20 +50,25 @@ TypeRefBuilder::getRemoteAddrOfTypeRefPointer(const void *pointer) {
50
50
51
51
TypeRefBuilder::TypeRefBuilder () : TC(*this ) {}
52
52
53
- // / Determine whether the given reflection protocol name matches.
54
- static bool reflectionNameMatches (Demangler &dem,
55
- StringRef reflectionName,
56
- StringRef searchName) {
53
+ // / Normalize a mangled name so it can be matched with string equality.
54
+ static std::string normalizeReflectionName (Demangler &dem, StringRef reflectionName) {
57
55
reflectionName = dropSwiftManglingPrefix (reflectionName);
58
56
59
57
// Remangle the reflection name to resolve symbolic references.
60
58
if (auto node = dem.demangleType (reflectionName)) {
61
- auto remangled = mangleNode (node);
62
- return remangled == searchName;
59
+ return mangleNode (node);
63
60
}
64
-
65
- // Fall back to string matching.
66
- return reflectionName.equals (searchName);
61
+
62
+ // Fall back to the raw string.
63
+ return reflectionName;
64
+ }
65
+
66
+ // / Determine whether the given reflection protocol name matches.
67
+ static bool reflectionNameMatches (Demangler &dem,
68
+ StringRef reflectionName,
69
+ StringRef searchName) {
70
+ auto normalized = normalizeReflectionName (dem, reflectionName);
71
+ return searchName.equals (normalized);
67
72
}
68
73
69
74
const TypeRef * TypeRefBuilder::
@@ -139,6 +144,12 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
139
144
else
140
145
return {};
141
146
147
+ // Try the cache.
148
+ auto Found = FieldTypeInfoCache.find (MangledName);
149
+ if (Found != FieldTypeInfoCache.end ())
150
+ return Found->second ;
151
+
152
+ // On failure, fill out the cache with everything we know about.
142
153
std::vector<std::pair<std::string, const TypeRef *>> Fields;
143
154
for (auto &Info : ReflectionInfos) {
144
155
uintptr_t TypeRefOffset = Info.Field .SectionOffset
@@ -147,12 +158,16 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
147
158
if (!FD.hasMangledTypeName ())
148
159
continue ;
149
160
auto CandidateMangledName = FD.getMangledTypeName (TypeRefOffset);
150
- if (!reflectionNameMatches (Dem, CandidateMangledName, MangledName))
151
- continue ;
152
- return {&FD, &Info};
161
+ auto NormalizedName = normalizeReflectionName (Dem, CandidateMangledName);
162
+ FieldTypeInfoCache[NormalizedName] = {&FD, &Info};
153
163
}
154
164
}
155
165
166
+ // We've filled the cache with everything we know about now. Try the cache again.
167
+ Found = FieldTypeInfoCache.find (MangledName);
168
+ if (Found != FieldTypeInfoCache.end ())
169
+ return Found->second ;
170
+
156
171
return {nullptr , 0 };
157
172
}
158
173
0 commit comments