Skip to content

Commit 964bd44

Browse files
committed
Remove unnecessary shared ownership of loaded metadata/dominator buffers.
1 parent 95cc452 commit 964bd44

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

include/swift/Remote/MetadataReader.h

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,10 @@
2424

2525
#include <vector>
2626
#include <unordered_map>
27-
#include <utility>
2827

2928
namespace swift {
3029
namespace remote {
3130

32-
template <typename Runtime>
33-
using SharedTargetMetadataRef = std::shared_ptr<TargetMetadata<Runtime>>;
34-
35-
using SharedCaptureDescriptor = std::shared_ptr<const CaptureDescriptor>;
36-
37-
template <typename Runtime>
38-
using SharedTargetNominalTypeDescriptorRef
39-
= std::shared_ptr<TargetNominalTypeDescriptor<Runtime>>;
40-
41-
template <typename Runtime>
42-
using SharedProtocolDescriptorRef
43-
= std::shared_ptr<TargetProtocolDescriptor<Runtime>>;
44-
4531
/// A utility class for constructing abstract types from
4632
/// a textual mangling.
4733
template <typename BuilderType>
@@ -351,6 +337,14 @@ class RemoteRef {
351337
}
352338
};
353339

340+
/// A structure, designed for use with std::unique_ptr, which destroys
341+
/// a pointer by calling free on it (and not trying to call a destructor).
342+
struct delete_with_free {
343+
void operator()(const void *memory) {
344+
free(const_cast<void*>(memory));
345+
}
346+
};
347+
354348
/// A generic reader of metadata.
355349
///
356350
/// BuilderType must implement a particular interface which is currently
@@ -373,22 +367,30 @@ class MetadataReader {
373367

374368
using MetadataRef =
375369
RemoteRef<Runtime, TargetMetadata<Runtime>>;
376-
using SharedMetadataRef =
377-
SharedTargetMetadataRef<Runtime>;
370+
using OwnedMetadataRef =
371+
std::unique_ptr<const TargetMetadata<Runtime>, delete_with_free>;
378372

379373
/// A cache of read type metadata, keyed by the address of the metadata.
380-
std::unordered_map<StoredPointer, SharedMetadataRef> MetadataCache;
374+
std::unordered_map<StoredPointer, OwnedMetadataRef>
375+
MetadataCache;
381376

382377
using NominalTypeDescriptorRef =
383378
RemoteRef<Runtime, TargetNominalTypeDescriptor<Runtime>>;
384-
using SharedNominalTypeDescriptorRef =
385-
SharedTargetNominalTypeDescriptorRef<Runtime>;
379+
using OwnedNominalTypeDescriptorRef =
380+
std::unique_ptr<const TargetNominalTypeDescriptor<Runtime>,
381+
delete_with_free>;
386382

387383
/// A cache of read nominal type descriptors, keyed by the address of the
388384
/// nominal type descriptor.
389-
std::unordered_map<StoredPointer, SharedNominalTypeDescriptorRef>
385+
std::unordered_map<StoredPointer, OwnedNominalTypeDescriptorRef>
390386
NominalTypeDescriptorCache;
391387

388+
using OwnedProtocolDescriptorRef =
389+
std::unique_ptr<const TargetProtocolDescriptor<Runtime>, delete_with_free>;
390+
391+
using OwnedCaptureDescriptor =
392+
std::unique_ptr<const CaptureDescriptor, delete_with_free>;
393+
392394
public:
393395
BuilderType Builder;
394396

@@ -421,21 +423,12 @@ class MetadataReader {
421423
}
422424

423425
/// Given a remote pointer to metadata, attempt to discover its MetadataKind.
424-
std::pair<bool,MetadataKind>
426+
std::pair<bool, MetadataKind>
425427
readKindFromMetadata(StoredPointer MetadataAddress) {
426-
auto Cached = MetadataCache.find(MetadataAddress);
427-
if (Cached != MetadataCache.end()) {
428-
if (auto Meta = Cached->second) {
429-
return {true,Cached->second->getKind()};
430-
} else {
431-
return {false,MetadataKind::Opaque};
432-
}
433-
}
434-
435-
auto Meta = readMetadata(MetadataAddress);
436-
if (!Meta) return {false,MetadataKind::Opaque};
428+
auto meta = readMetadata(MetadataAddress);
429+
if (!meta) return {false, MetadataKind::Opaque};
437430

438-
return {true,Meta->getKind()};
431+
return {true, meta->getKind()};
439432
}
440433

441434
/// Given a remote pointer to metadata, attempt to turn it into a type.
@@ -590,7 +583,7 @@ class MetadataReader {
590583
}
591584

592585
auto metadata = reinterpret_cast<TargetMetadata<Runtime>*>(buffer);
593-
MetadataCache.insert({address, SharedMetadataRef(metadata, free)});
586+
MetadataCache.insert(std::make_pair(address, OwnedMetadataRef(metadata)));
594587
return MetadataRef(address, metadata);
595588
}
596589

@@ -703,8 +696,8 @@ class MetadataReader {
703696
auto descriptor
704697
= reinterpret_cast<TargetNominalTypeDescriptor<Runtime> *>(buffer);
705698

706-
NominalTypeDescriptorCache.insert({address,
707-
SharedNominalTypeDescriptorRef(descriptor, free)});
699+
NominalTypeDescriptorCache.insert(
700+
std::make_pair(address, OwnedNominalTypeDescriptorRef(descriptor)));
708701
return NominalTypeDescriptorRef(address, descriptor);
709702
}
710703

@@ -724,7 +717,7 @@ class MetadataReader {
724717
return decl;
725718
}
726719

727-
SharedProtocolDescriptorRef<Runtime>
720+
OwnedProtocolDescriptorRef
728721
readProtocolDescriptor(StoredPointer Address) {
729722
auto Size = sizeof(TargetProtocolDescriptor<Runtime>);
730723
auto Buffer = (uint8_t *)malloc(Size);
@@ -734,7 +727,7 @@ class MetadataReader {
734727
}
735728
auto Casted
736729
= reinterpret_cast<TargetProtocolDescriptor<Runtime> *>(Buffer);
737-
return SharedProtocolDescriptorRef<Runtime>(Casted, free);
730+
return OwnedProtocolDescriptorRef(Casted);
738731
}
739732

740733
StoredPointer getNominalParent(MetadataRef metadata,
@@ -830,7 +823,7 @@ class MetadataReader {
830823
/// Read the entire CaptureDescriptor in this address space, including
831824
/// trailing capture typeref relative offsets, and GenericMetadataSource
832825
/// pairs.
833-
SharedCaptureDescriptor readCaptureDescriptor(StoredPointer Address) {
826+
OwnedCaptureDescriptor readCaptureDescriptor(StoredPointer Address) {
834827

835828
uint32_t NumCaptures = 0;
836829
uint32_t NumMetadataSources = 0;
@@ -857,7 +850,7 @@ class MetadataReader {
857850
}
858851

859852
auto RawDescriptor = reinterpret_cast<const CaptureDescriptor *>(Buffer);
860-
return SharedCaptureDescriptor(RawDescriptor, /*deleter*/ free);
853+
return OwnedCaptureDescriptor(RawDescriptor);
861854
}
862855
};
863856

0 commit comments

Comments
 (0)