Skip to content

Commit 28bbc54

Browse files
committed
Switch the generic heap box metadata cache over to use ConcurrentMap directly.
1 parent 81b27c2 commit 28bbc54

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

stdlib/public/runtime/HeapObject.cpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Basic/Lazy.h"
18+
#include "swift/Runtime/Concurrent.h"
1819
#include "swift/Runtime/HeapObject.h"
1920
#include "swift/Runtime/Heap.h"
2021
#include "swift/Runtime/Metadata.h"
2122
#include "swift/ABI/System.h"
2223
#include "llvm/Support/MathExtras.h"
23-
#include "MetadataCache.h"
2424
#include "Private.h"
2525
#include "swift/Runtime/Debug.h"
2626
#include <algorithm>
@@ -145,36 +145,36 @@ static void destroyGenericBox(HeapObject *o) {
145145
metadata->getAllocAlignMask());
146146
}
147147

148-
class BoxCacheEntry : public CacheEntry<BoxCacheEntry> {
148+
class BoxCacheEntry {
149149
public:
150-
FullMetadata<GenericBoxHeapMetadata> Metadata;
150+
FullMetadata<GenericBoxHeapMetadata> Data;
151151

152-
BoxCacheEntry(size_t numArguments)
153-
: Metadata{HeapMetadataHeader{{destroyGenericBox}, {nullptr}},
154-
GenericBoxHeapMetadata{MetadataKind::HeapGenericLocalVariable, 0,
155-
nullptr}} {
156-
assert(numArguments == 1);
152+
BoxCacheEntry(const Metadata *type)
153+
: Data{HeapMetadataHeader{{destroyGenericBox}, {/*vwtable*/ nullptr}},
154+
GenericBoxHeapMetadata{MetadataKind::HeapGenericLocalVariable,
155+
GenericBoxHeapMetadata::getHeaderOffset(type),
156+
type}} {
157157
}
158158

159-
size_t getNumArguments() const {
160-
return 1;
159+
long getKeyIntValueForDump() {
160+
return reinterpret_cast<long>(Data.BoxedType);
161161
}
162162

163-
static const char *getName() {
164-
return "BoxCache";
163+
int compareWithKey(const Metadata *type) const {
164+
return comparePointers(type, Data.BoxedType);
165165
}
166166

167-
FullMetadata<GenericBoxHeapMetadata> *getData() {
168-
return &Metadata;
167+
static size_t getExtraAllocationSize(const Metadata *key) {
168+
return 0;
169169
}
170-
const FullMetadata<GenericBoxHeapMetadata> *getData() const {
171-
return &Metadata;
170+
size_t getExtraAllocationSize() const {
171+
return 0;
172172
}
173173
};
174174

175175
} // end anonymous namespace
176176

177-
static Lazy<MetadataCache<BoxCacheEntry>> Boxes;
177+
static ConcurrentMap<BoxCacheEntry, false> Boxes;
178178

179179
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT
180180
BoxPair::Return
@@ -186,20 +186,7 @@ SWIFT_CC(swift) SWIFT_RT_ENTRY_IMPL_VISIBILITY
186186
extern "C"
187187
BoxPair::Return SWIFT_RT_ENTRY_IMPL(swift_allocBox)(const Metadata *type) {
188188
// Get the heap metadata for the box.
189-
auto &B = Boxes.get();
190-
const void *typeArg = type;
191-
auto entry = B.findOrAdd(&typeArg, 1, [&]() -> BoxCacheEntry* {
192-
// Create a new entry for the box.
193-
auto entry = BoxCacheEntry::allocate(B.getAllocator(), &typeArg, 1, 0);
194-
195-
auto metadata = entry->getData();
196-
metadata->Offset = GenericBoxHeapMetadata::getHeaderOffset(type);
197-
metadata->BoxedType = type;
198-
199-
return entry;
200-
});
201-
202-
auto metadata = entry->getData();
189+
auto metadata = &Boxes.getOrInsert(type).first->Data;
203190

204191
// Allocate and project the box.
205192
auto allocation = SWIFT_RT_ENTRY_CALL(swift_allocObject)(

0 commit comments

Comments
 (0)