Skip to content

[Runtime] Reimplement protocol conformance cache with a hash table #33487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ class ReflectionContext
std::function<void(StoredPointer Type, StoredPointer Proto)> Call) {
if (!NodePtr)
return;
auto NodeBytes = getReader().readBytes(RemoteAddress(NodePtr), sizeof(Node));
auto NodeBytes = getReader().readBytes(RemoteAddress(NodePtr),
sizeof(ConformanceNode<Runtime>));
auto NodeData =
reinterpret_cast<const ConformanceNode<Runtime> *>(NodeBytes.get());
if (!NodeData)
Expand All @@ -889,6 +890,33 @@ class ReflectionContext
iterateConformanceTree(NodeData->Right, Call);
}

void IterateConformanceTable(
RemoteAddress ConformancesPtr,
std::function<void(StoredPointer Type, StoredPointer Proto)> Call) {
auto MapBytes = getReader().readBytes(RemoteAddress(ConformancesPtr),
sizeof(ConcurrentHashMap<Runtime>));
auto MapData =
reinterpret_cast<const ConcurrentHashMap<Runtime> *>(MapBytes.get());
if (!MapData)
return;

auto Count = MapData->ElementCount;
auto Size = Count * sizeof(ConformanceCacheEntry<Runtime>);

auto ElementsBytes =
getReader().readBytes(RemoteAddress(MapData->Elements), Size);
auto ElementsData =
reinterpret_cast<const ConformanceCacheEntry<Runtime> *>(
ElementsBytes.get());
if (!ElementsData)
return;

for (StoredSize i = 0; i < Count; i++) {
auto &Element = ElementsData[i];
Call(Element.Type, Element.Proto);
}
}

/// Iterate the protocol conformance cache in the target process, calling Call
/// with the type and protocol of each conformance. Returns None on success,
/// and a string describing the error on failure.
Expand All @@ -908,7 +936,26 @@ class ReflectionContext

auto Root = getReader().readPointer(ConformancesAddr->getResolvedAddress(),
sizeof(StoredPointer));
iterateConformanceTree(Root->getResolvedAddress().getAddressData(), Call);
auto ReaderCount = Root->getResolvedAddress().getAddressData();

// ReaderCount will be the root pointer if the conformance cache is a
// ConcurrentMap. It's very unlikely that there would ever be more readers
// than the least valid pointer value, so compare with that to distinguish.
// TODO: once the old conformance cache is gone for good, remove that code.
uint64_t LeastValidPointerValue;
if (!getReader().queryDataLayout(
DataLayoutQueryType::DLQ_GetLeastValidPointerValue, nullptr,
&LeastValidPointerValue)) {
return std::string("unable to query least valid pointer value");
}

if (ReaderCount < LeastValidPointerValue)
IterateConformanceTable(ConformancesAddr->getResolvedAddress(), Call);
else {
// The old code has the root address at this location.
auto RootAddr = ReaderCount;
iterateConformanceTree(RootAddr, Call);
}
return llvm::None;
}

Expand Down
14 changes: 14 additions & 0 deletions include/swift/Reflection/RuntimeInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ template <typename Runtime> struct MetadataCacheNode {
typename Runtime::StoredPointer Right;
};

template <typename Runtime> struct ConcurrentHashMap {
typename Runtime::StoredSize ReaderCount;
typename Runtime::StoredSize ElementCount;
typename Runtime::StoredPointer Elements;
typename Runtime::StoredPointer Indices;
// We'll ignore the remaining fields for now....
};

template <typename Runtime> struct ConformanceCacheEntry {
typename Runtime::StoredPointer Type;
typename Runtime::StoredPointer Proto;
typename Runtime::StoredPointer Witness;
};

} // end namespace reflection
} // end namespace swift

Expand Down
Loading