17
17
#ifndef SWIFT_REMOTE_METADATAREADER_H
18
18
#define SWIFT_REMOTE_METADATAREADER_H
19
19
20
+ #include " llvm/ADT/Hashing.h"
21
+
20
22
#include " swift/Runtime/Metadata.h"
21
23
#include " swift/Remote/MemoryReader.h"
22
24
#include " swift/Demangling/Demangler.h"
@@ -193,8 +195,18 @@ class MetadataReader {
193
195
// / amounts of data when we encounter corrupt values for sizes/counts.
194
196
static const uint64_t MaxMetadataSize = 1048576 ; // 1MB
195
197
196
- // / A cache of built types, keyed by the address of the type.
197
- std::unordered_map<StoredPointer, BuiltType> TypeCache;
198
+ // / Define a has function for a std::pair<StoredPointer, bool>
199
+ struct TypeCacheKeyHash {
200
+ std::size_t operator ()(const std::pair<StoredPointer, bool > &x) const {
201
+ return llvm::hash_combine (x.first , x.second );
202
+ }
203
+ };
204
+
205
+ // / A cache of built types, keyed by the address of the type and whether the
206
+ // / request ignored articial superclasses or not.
207
+ std::unordered_map<std::pair<StoredPointer, bool >, BuiltType,
208
+ TypeCacheKeyHash>
209
+ TypeCache;
198
210
199
211
using MetadataRef = RemoteRef<const TargetMetadata<Runtime>>;
200
212
using OwnedMetadataRef = MemoryReader::ReadBytesResult;
@@ -821,7 +833,9 @@ class MetadataReader {
821
833
readTypeFromMetadata (StoredPointer MetadataAddress,
822
834
bool skipArtificialSubclasses = false ,
823
835
int recursion_limit = defaultTypeRecursionLimit) {
824
- auto Cached = TypeCache.find (MetadataAddress);
836
+ std::pair<StoredPointer, bool > TypeCacheKey (MetadataAddress,
837
+ skipArtificialSubclasses);
838
+ auto Cached = TypeCache.find (TypeCacheKey);
825
839
if (Cached != TypeCache.end ())
826
840
return Cached->second ;
827
841
@@ -841,7 +855,7 @@ class MetadataReader {
841
855
// Insert a negative result into the cache now so that, if we recur with
842
856
// the same address, we will return the negative result with the check
843
857
// just above.
844
- TypeCache.insert ({MetadataAddress , BuiltType ()});
858
+ TypeCache.insert ({TypeCacheKey , BuiltType ()});
845
859
846
860
auto Meta = readMetadata (MetadataAddress);
847
861
if (!Meta) return BuiltType ();
@@ -890,7 +904,7 @@ class MetadataReader {
890
904
891
905
auto BuiltTuple =
892
906
Builder.createTupleType (elementTypes, labels);
893
- TypeCache[MetadataAddress ] = BuiltTuple;
907
+ TypeCache[TypeCacheKey ] = BuiltTuple;
894
908
return BuiltTuple;
895
909
}
896
910
case MetadataKind::Function: {
@@ -947,7 +961,7 @@ class MetadataReader {
947
961
948
962
auto BuiltFunction = Builder.createFunctionType (
949
963
Parameters, Result, flags, diffKind, globalActor);
950
- TypeCache[MetadataAddress ] = BuiltFunction;
964
+ TypeCache[TypeCacheKey ] = BuiltFunction;
951
965
return BuiltFunction;
952
966
}
953
967
case MetadataKind::Existential: {
@@ -998,7 +1012,7 @@ class MetadataReader {
998
1012
}
999
1013
auto BuiltExist = Builder.createProtocolCompositionType (
1000
1014
Protocols, SuperclassType, HasExplicitAnyObject);
1001
- TypeCache[MetadataAddress ] = BuiltExist;
1015
+ TypeCache[TypeCacheKey ] = BuiltExist;
1002
1016
return BuiltExist;
1003
1017
}
1004
1018
case MetadataKind::ExtendedExistential: {
@@ -1076,7 +1090,7 @@ class MetadataReader {
1076
1090
}
1077
1091
}
1078
1092
1079
- TypeCache[MetadataAddress ] = builtProto;
1093
+ TypeCache[TypeCacheKey ] = builtProto;
1080
1094
return builtProto;
1081
1095
}
1082
1096
@@ -1086,7 +1100,7 @@ class MetadataReader {
1086
1100
readTypeFromMetadata (Metatype->InstanceType , false , recursion_limit);
1087
1101
if (!Instance) return BuiltType ();
1088
1102
auto BuiltMetatype = Builder.createMetatypeType (Instance);
1089
- TypeCache[MetadataAddress ] = BuiltMetatype;
1103
+ TypeCache[TypeCacheKey ] = BuiltMetatype;
1090
1104
return BuiltMetatype;
1091
1105
}
1092
1106
case MetadataKind::ObjCClassWrapper: {
@@ -1098,7 +1112,7 @@ class MetadataReader {
1098
1112
return BuiltType ();
1099
1113
1100
1114
auto BuiltObjCClass = Builder.createObjCClassType (std::move (className));
1101
- TypeCache[MetadataAddress ] = BuiltObjCClass;
1115
+ TypeCache[TypeCacheKey ] = BuiltObjCClass;
1102
1116
return BuiltObjCClass;
1103
1117
}
1104
1118
case MetadataKind::ExistentialMetatype: {
@@ -1107,7 +1121,7 @@ class MetadataReader {
1107
1121
readTypeFromMetadata (Exist->InstanceType , false , recursion_limit);
1108
1122
if (!Instance) return BuiltType ();
1109
1123
auto BuiltExist = Builder.createExistentialMetatypeType (Instance);
1110
- TypeCache[MetadataAddress ] = BuiltExist;
1124
+ TypeCache[TypeCacheKey ] = BuiltExist;
1111
1125
return BuiltExist;
1112
1126
}
1113
1127
case MetadataKind::ForeignReferenceType:
@@ -1131,7 +1145,7 @@ class MetadataReader {
1131
1145
auto name = mangling.result ();
1132
1146
1133
1147
auto BuiltForeign = Builder.createForeignClassType (std::move (name));
1134
- TypeCache[MetadataAddress ] = BuiltForeign;
1148
+ TypeCache[TypeCacheKey ] = BuiltForeign;
1135
1149
return BuiltForeign;
1136
1150
}
1137
1151
case MetadataKind::HeapLocalVariable:
@@ -1142,7 +1156,7 @@ class MetadataReader {
1142
1156
case MetadataKind::Opaque:
1143
1157
default : {
1144
1158
auto BuiltOpaque = Builder.getOpaqueType ();
1145
- TypeCache[MetadataAddress ] = BuiltOpaque;
1159
+ TypeCache[TypeCacheKey ] = BuiltOpaque;
1146
1160
return BuiltOpaque;
1147
1161
}
1148
1162
}
@@ -3084,9 +3098,12 @@ class MetadataReader {
3084
3098
// If we've skipped an artificial subclasses, check the cache at
3085
3099
// the superclass. (This also protects against recursion.)
3086
3100
if (skipArtificialSubclasses && metadata != origMetadata) {
3087
- auto it = TypeCache.find (getAddress (metadata));
3088
- if (it != TypeCache.end ())
3101
+ auto it =
3102
+ TypeCache.find ({getAddress (metadata), skipArtificialSubclasses});
3103
+ if (it != TypeCache.end ()) {
3104
+ TypeCache.erase ({getAddress (origMetadata), skipArtificialSubclasses});
3089
3105
return it->second ;
3106
+ }
3090
3107
}
3091
3108
3092
3109
// Read the nominal type descriptor.
@@ -3115,12 +3132,12 @@ class MetadataReader {
3115
3132
if (!nominal)
3116
3133
return BuiltType ();
3117
3134
3118
- TypeCache[getAddress (metadata)] = nominal;
3135
+ TypeCache[{ getAddress (metadata), skipArtificialSubclasses} ] = nominal;
3119
3136
3120
3137
// If we've skipped an artificial subclass, remove the
3121
3138
// recursion-protection entry we made for it.
3122
3139
if (skipArtificialSubclasses && metadata != origMetadata) {
3123
- TypeCache.erase (getAddress (origMetadata));
3140
+ TypeCache.erase ({ getAddress (origMetadata), skipArtificialSubclasses} );
3124
3141
}
3125
3142
3126
3143
return nominal;
@@ -3151,7 +3168,7 @@ class MetadataReader {
3151
3168
skipArtificialSubclasses, recursion_limit);
3152
3169
}
3153
3170
3154
- TypeCache[origMetadataPtr] = BuiltObjCClass;
3171
+ TypeCache[{ origMetadataPtr, skipArtificialSubclasses} ] = BuiltObjCClass;
3155
3172
return BuiltObjCClass;
3156
3173
}
3157
3174
0 commit comments