Skip to content

Commit 79cdadf

Browse files
authored
Merge pull request #68465 from augusto2112/refl-dense-map
[NFC] Replace std::unordered_map with llvm::DenseSet in metadata reader
2 parents 58b0ea9 + 5282464 commit 79cdadf

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

include/swift/Remote/MetadataReader.h

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#ifndef SWIFT_REMOTE_METADATAREADER_H
1818
#define SWIFT_REMOTE_METADATAREADER_H
1919

20-
#include "llvm/ADT/Hashing.h"
2120

2221
#include "swift/Runtime/Metadata.h"
2322
#include "swift/Remote/MemoryReader.h"
@@ -34,7 +33,6 @@
3433

3534
#include <type_traits>
3635
#include <vector>
37-
#include <unordered_map>
3836

3937
#include <inttypes.h>
4038

@@ -195,25 +193,43 @@ class MetadataReader {
195193
/// amounts of data when we encounter corrupt values for sizes/counts.
196194
static const uint64_t MaxMetadataSize = 1048576; // 1MB
197195

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);
196+
/// The dense map info for a std::pair<StoredPointer, bool>.
197+
struct DenseMapInfoTypeCacheKey {
198+
using Pair = std::pair<StoredPointer, bool>;
199+
using StoredPointerInfo = llvm::DenseMapInfo<StoredPointer>;
200+
201+
static inline Pair getEmptyKey() {
202+
// Since bool doesn't have an empty key implementation, we only use the
203+
// StoredPointer's empty key.
204+
return std::make_pair(StoredPointerInfo::getEmptyKey(), false);
205+
}
206+
207+
static inline Pair getTombstoneKey() {
208+
// Since bool doesn't have a tombstone key implementation, we only use the
209+
// StoredPointer's tombstone key.
210+
return std::make_pair(StoredPointerInfo::getTombstoneKey(), false);
211+
}
212+
213+
static unsigned getHashValue(const Pair &PairVal) {
214+
return llvm::hash_combine(PairVal.first, PairVal.second);
215+
}
216+
217+
static bool isEqual(const Pair &LHS, const Pair &RHS) {
218+
return LHS.first == RHS.first && LHS.second == RHS.second;
202219
}
203220
};
204221

205222
/// A cache of built types, keyed by the address of the type and whether the
206223
/// request ignored articial superclasses or not.
207-
std::unordered_map<std::pair<StoredPointer, bool>, BuiltType,
208-
TypeCacheKeyHash>
224+
llvm::DenseMap<std::pair<StoredPointer, bool>, BuiltType,
225+
DenseMapInfoTypeCacheKey>
209226
TypeCache;
210227

211228
using MetadataRef = RemoteRef<const TargetMetadata<Runtime>>;
212229
using OwnedMetadataRef = MemoryReader::ReadBytesResult;
213230

214231
/// A cache of read type metadata, keyed by the address of the metadata.
215-
std::unordered_map<StoredPointer, OwnedMetadataRef>
216-
MetadataCache;
232+
llvm::DenseMap<StoredPointer, OwnedMetadataRef> MetadataCache;
217233

218234
using ContextDescriptorRef =
219235
RemoteRef<const TargetContextDescriptor<Runtime>>;
@@ -295,15 +311,14 @@ class MetadataReader {
295311

296312
/// A cache of read nominal type descriptors, keyed by the address of the
297313
/// nominal type descriptor.
298-
std::unordered_map<StoredPointer, OwnedContextDescriptorRef>
299-
ContextDescriptorCache;
314+
llvm::DenseMap<StoredPointer, OwnedContextDescriptorRef>
315+
ContextDescriptorCache;
300316

301317
using OwnedProtocolDescriptorRef =
302318
std::unique_ptr<const TargetProtocolDescriptor<Runtime>, delete_with_free>;
303319
/// A cache of read extended existential shape metadata, keyed by the
304320
/// address of the shape metadata.
305-
std::unordered_map<StoredPointer, OwnedShapeRef>
306-
ShapeCache;
321+
llvm::DenseMap<StoredPointer, OwnedShapeRef> ShapeCache;
307322

308323
enum class IsaEncodingKind {
309324
/// We haven't checked yet.

include/swift/RemoteInspection/ReflectionContext.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include "swift/Basic/Unreachable.h"
4040

4141
#include <set>
42-
#include <unordered_map>
4342
#include <utility>
4443
#include <vector>
4544

@@ -122,8 +121,7 @@ class ReflectionContext
122121
using super::readMetadata;
123122
using super::readObjCClassName;
124123
using super::readResolvedPointerValue;
125-
std::unordered_map<typename super::StoredPointer, const RecordTypeInfo *>
126-
Cache;
124+
llvm::DenseMap<typename super::StoredPointer, const RecordTypeInfo *> Cache;
127125

128126
/// All buffers we need to keep around long term. This will automatically free them
129127
/// when this object is destroyed.

0 commit comments

Comments
 (0)