@@ -190,8 +190,8 @@ GetTypeAlias(swift::Demangle::Demangler &dem,
190
190
}
191
191
192
192
// / Find a Clang type by name in the modules in \p module_holder.
193
- TypeSP TypeSystemSwiftTypeRef::LookupClangType (StringRef name ) {
194
- auto lookup = [](Module &M, StringRef name) -> TypeSP {
193
+ TypeSP TypeSystemSwiftTypeRef::LookupClangType (StringRef name_ref ) {
194
+ auto lookup = [](Module &M, ConstString name) -> TypeSP {
195
195
llvm::SmallVector<CompilerContext, 2 > decl_context;
196
196
decl_context.push_back ({CompilerContextKind::AnyModule, ConstString ()});
197
197
decl_context.push_back ({CompilerContextKind::AnyType, ConstString (name)});
@@ -203,18 +203,33 @@ TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) {
203
203
return {};
204
204
return clang_types.GetTypeAtIndex (0 );
205
205
};
206
- if (auto *M = GetModule ())
207
- return lookup (*M, name);
206
+
207
+ // Check the cache first. Negative results are also cached.
208
+ TypeSP result;
209
+ ConstString name (name_ref);
210
+ if (m_clang_type_cache.Lookup (name.AsCString (), result))
211
+ return result;
212
+
213
+ if (auto *M = GetModule ()) {
214
+ TypeSP result = lookup (*M, name);
215
+ // Cache it.
216
+ m_clang_type_cache.Insert (name.AsCString (), result);
217
+ return result;
218
+ }
208
219
209
220
SwiftASTContext *target_holder = GetSwiftASTContext ();
210
221
if (!target_holder)
211
222
return {};
212
223
TargetSP target_sp = target_holder->GetTargetWP ().lock ();
213
224
if (!target_sp)
214
225
return {};
215
- TypeSP result;
216
226
target_sp->GetImages ().ForEach ([&](const ModuleSP &module ) -> bool {
227
+ // Don't recursively call into LookupClangTypes() to avoid filling
228
+ // hundreds of image caches with negative results.
217
229
result = lookup (const_cast <Module &>(*module ), name);
230
+ // Cache it in the expression context.
231
+ if (result)
232
+ m_clang_type_cache.Insert (name.AsCString (), result);
218
233
return !result;
219
234
});
220
235
return result;
0 commit comments