Skip to content

Commit 9288b71

Browse files
committed
[MetadataLookup] Enable field descriptor cache
1 parent c8e3724 commit 9288b71

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,6 @@ static Demangler getDemanglerForRuntimeTypeResolution() {
6464
return dem;
6565
}
6666

67-
template <typename T> struct DescriptorCacheEntry {
68-
private:
69-
std::string Name;
70-
const T *Description;
71-
72-
public:
73-
DescriptorCacheEntry(const llvm::StringRef name, const T *description)
74-
: Name(name.str()), Description(description) {}
75-
76-
const T *getDescription() { return Description; }
77-
78-
int compareWithKey(llvm::StringRef aName) const {
79-
return aName.compare(Name);
80-
}
81-
82-
template <class... Args>
83-
static size_t getExtraAllocationSize(Args &&... ignored) {
84-
return 0;
85-
}
86-
};
87-
8867
#pragma mark Nominal type descriptor cache
8968
// Type Metadata Cache.
9069

@@ -333,8 +312,30 @@ namespace {
333312
}
334313
};
335314

315+
struct ProtocolDescriptorCacheEntry {
316+
private:
317+
std::string Name;
318+
const ProtocolDescriptor *Description;
319+
320+
public:
321+
ProtocolDescriptorCacheEntry(const llvm::StringRef name,
322+
const ProtocolDescriptor *description)
323+
: Name(name.str()), Description(description) {}
324+
325+
const ProtocolDescriptor *getDescription() { return Description; }
326+
327+
int compareWithKey(llvm::StringRef aName) const {
328+
return aName.compare(Name);
329+
}
330+
331+
template <class... T>
332+
static size_t getExtraAllocationSize(T &&... ignored) {
333+
return 0;
334+
}
335+
};
336+
336337
struct ProtocolMetadataState {
337-
ConcurrentMap<DescriptorCacheEntry<ProtocolDescriptor>> ProtocolCache;
338+
ConcurrentMap<ProtocolDescriptorCacheEntry> ProtocolCache;
338339
std::vector<ProtocolSection> SectionsToScan;
339340
Mutex SectionsToScanLock;
340341

@@ -425,19 +426,22 @@ _findProtocolDescriptor(llvm::StringRef mangledName) {
425426

426427
#pragma mark Type field descriptor cache
427428
namespace {
428-
template <typename T> struct FieldDescriptorCacheEntry {
429+
struct FieldDescriptorCacheEntry {
429430
private:
430-
const Metadata *Base;
431-
const T *Description;
431+
const Metadata *Type;
432+
const FieldDescriptor *Description;
432433

433434
public:
434-
FieldDescriptorCacheEntry(const Metadata *Base, const T *description)
435-
: Base(Base), Description(description) {}
435+
FieldDescriptorCacheEntry(const Metadata *type,
436+
const FieldDescriptor *description)
437+
: Type(type), Description(description) {}
436438

437-
const T *getDescription() { return Description; }
439+
const FieldDescriptor *getDescription() { return Description; }
438440

439441
int compareWithKey(const Metadata *other) const {
440-
return Base == other;
442+
auto a = (uintptr_t)Type;
443+
auto b = (uintptr_t)other;
444+
return a == b ? 0 : (a < b ? -1 : 1);
441445
}
442446

443447
template <class... Args>
@@ -477,7 +481,7 @@ class DynamicFieldSection {
477481
};
478482

479483
struct FieldCacheState {
480-
ConcurrentMap<FieldDescriptorCacheEntry<FieldDescriptor>> FieldCache;
484+
ConcurrentMap<FieldDescriptorCacheEntry> FieldCache;
481485

482486
Mutex SectionsLock;
483487
std::vector<StaticFieldSection> StaticSections;
@@ -1122,12 +1126,10 @@ void swift::swift_getFieldAt(
11221126

11231127

11241128
// Fast path: If we already have field descriptor cached.
1125-
/*
11261129
if (auto Value = cache.FieldCache.find(base)) {
11271130
getFieldAt(*Value->getDescription());
11281131
return;
11291132
}
1130-
*/
11311133

11321134
ScopedLock guard(cache.SectionsLock);
11331135
// Otherwise let's try to find it in one of the sections.

0 commit comments

Comments
 (0)