Skip to content

Commit 981c358

Browse files
authored
Merge pull request swiftlang#40432 from augusto2112/lazy-typeref-caching
Make caching the typeref builder cache lazier
2 parents eda3824 + d200a06 commit 981c358

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,10 @@ class TypeRefBuilder {
643643

644644
private:
645645
std::vector<ReflectionInfo> ReflectionInfos;
646+
647+
/// Index of the next Reflection Info that should be processed.
648+
/// This assumes that Reflection Infos are never removed from the vector.
649+
size_t FirstUnprocessedReflectionInfoIndex = 0;
646650

647651
llvm::Optional<std::string> normalizeReflectionName(RemoteRef<char> name);
648652
bool reflectionNameMatches(RemoteRef<char> reflectionName,

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,25 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
201201
if (Found != FieldTypeInfoCache.end())
202202
return Found->second;
203203

204-
// On failure, fill out the cache with everything we know about.
205-
std::vector<std::pair<std::string, const TypeRef *>> Fields;
206-
for (auto &Info : ReflectionInfos) {
204+
// On failure, fill out the cache, ReflectionInfo by ReflectionInfo,
205+
// until we find the field desciptor we're looking for.
206+
while (FirstUnprocessedReflectionInfoIndex < ReflectionInfos.size()) {
207+
auto &Info = ReflectionInfos[FirstUnprocessedReflectionInfoIndex];
207208
for (auto FD : Info.Field) {
208209
if (!FD->hasMangledTypeName())
209210
continue;
210211
auto CandidateMangledName = readTypeRef(FD, FD->MangledTypeName);
211212
if (auto NormalizedName = normalizeReflectionName(CandidateMangledName))
212213
FieldTypeInfoCache[*NormalizedName] = FD;
213214
}
214-
}
215215

216-
// We've filled the cache with everything we know about now. Try the cache again.
217-
Found = FieldTypeInfoCache.find(MangledName);
218-
if (Found != FieldTypeInfoCache.end())
219-
return Found->second;
216+
// Since we're done with the current ReflectionInfo, increment early in
217+
// case we get a cache hit.
218+
++FirstUnprocessedReflectionInfoIndex;
219+
Found = FieldTypeInfoCache.find(MangledName);
220+
if (Found != FieldTypeInfoCache.end())
221+
return Found->second;
222+
}
220223

221224
return nullptr;
222225
}

0 commit comments

Comments
 (0)