Skip to content

Commit a1c4902

Browse files
committed
Enable the use of find_as with ActiveRequest
This avoids having to allocate a full AnyRequest for a hash lookup.
1 parent 35ba3d7 commit a1c4902

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

include/swift/AST/AnyRequest.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ class AnyRequest final : public AnyRequestBase<AnyRequest> {
292292

293293
const void *getRawStorage() const { return storage; }
294294

295+
/// Whether this wrapper is storing the same underlying request as an
296+
/// \c ActiveRequest.
297+
bool isStorageEqual(const ActiveRequest &other) const {
298+
// If either wrapper isn't storing anything, just return false.
299+
if (!hasStorage() || !other.hasStorage())
300+
return false;
301+
302+
if (getVTable()->typeID != other.getVTable()->typeID)
303+
return false;
304+
305+
return getVTable()->isEqual(getRawStorage(), other.getRawStorage());
306+
}
307+
295308
public:
296309
AnyRequest(const AnyRequest &other) : AnyRequestBase(other) {
297310
if (hasStorage()) {
@@ -382,6 +395,8 @@ namespace llvm {
382395
template<>
383396
struct DenseMapInfo<swift::AnyRequest> {
384397
using AnyRequest = swift::AnyRequest;
398+
using ActiveRequest = swift::ActiveRequest;
399+
385400
static inline AnyRequest getEmptyKey() {
386401
return AnyRequest(AnyRequest::StorageKind::Empty);
387402
}
@@ -396,6 +411,9 @@ namespace llvm {
396411
return AnyRequest::hashForHolder(swift::TypeID<Request>::value,
397412
hash_value(request));
398413
}
414+
static unsigned getHashValue(const ActiveRequest &request) {
415+
return hash_value(request);
416+
}
399417
static bool isEqual(const AnyRequest &lhs, const AnyRequest &rhs) {
400418
return lhs == rhs;
401419
}
@@ -407,6 +425,9 @@ namespace llvm {
407425
auto *rhsRequest = rhs.getAs<Request>();
408426
return rhsRequest && lhs == *rhsRequest;
409427
}
428+
static bool isEqual(const ActiveRequest &lhs, const AnyRequest &rhs) {
429+
return rhs.isStorageEqual(lhs);
430+
}
410431
};
411432

412433
} // end namespace llvm

lib/AST/Evaluator.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ bool Evaluator::checkDependency(const ActiveRequest &request) {
8080
dependencies.insert({req, {}});
8181

8282
// If there is an active request, record it's dependency on this request.
83-
if (!activeRequests.empty())
84-
dependencies[AnyRequest(activeRequests.back())].push_back(req);
83+
if (!activeRequests.empty()) {
84+
auto activeDeps = dependencies.find_as(activeRequests.back());
85+
assert(activeDeps != dependencies.end());
86+
activeDeps->second.push_back(req);
87+
}
8588
}
8689

8790
// Record this as an active request.

0 commit comments

Comments
 (0)