Skip to content

[swift-api-digester] caching the equality comparison results among SDKNodes. (#5297) #5391

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
Oct 21, 2016
Merged
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
44 changes: 8 additions & 36 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,45 +808,17 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
return Result;
}

/// This is for caching the comparison results between two SDKNodes.
class SDKNodeEqualContext {
using NodePtrAndEqual = llvm::DenseMap<const SDKNode*, bool>;
llvm::DenseMap<const SDKNode*, llvm::DenseMap<const SDKNode*, bool>> Data;

public:
Optional<bool> getEquality(const SDKNode* Left, const SDKNode* Right) {
auto &Map = Data.insert({Left, NodePtrAndEqual()}).first->getSecond();
if (Map.count(Right))
return Map[Right];
return None;
}

void addEquality(const SDKNode* Left, const SDKNode* Right, const bool Value) {
Data.insert(std::make_pair(Left, NodePtrAndEqual())).first->getSecond().
insert({Right, Value});
}
};

bool SDKNode::operator==(const SDKNode &Other) const {
static SDKNodeEqualContext EqualCache;
if (auto Cached = EqualCache.getEquality(this, &Other)) {
return Cached.getValue();
}
auto Exit = [&](const bool Result) {
EqualCache.addEquality(this, &Other, Result);
return Result;
};

if (getKind() != Other.getKind())
return Exit(false);
return false;

switch(getKind()) {
case SDKNodeKind::TypeNominal:
case SDKNodeKind::TypeFunc: {
auto Left = this->getAs<SDKNodeType>();
auto Right = (&Other)->getAs<SDKNodeType>();
return Exit(Left->getTypeAttributes().equals(Right->getTypeAttributes())
&& Left->getPrintedName() == Right->getPrintedName());
return Left->getTypeAttributes().equals(Right->getTypeAttributes())
&& Left->getPrintedName() == Right->getPrintedName();
}

case SDKNodeKind::Function:
Expand All @@ -856,9 +828,9 @@ bool SDKNode::operator==(const SDKNode &Other) const {
auto Left = this->getAs<SDKNodeAbstractFunc>();
auto Right = (&Other)->getAs<SDKNodeAbstractFunc>();
if (Left->isMutating() ^ Right->isMutating())
return Exit(false);
return false;
if (Left->isThrowing() ^ Right->isThrowing())
return Exit(false);
return false;
SWIFT_FALLTHROUGH;
}
case SDKNodeKind::TypeDecl:
Expand All @@ -870,11 +842,11 @@ bool SDKNode::operator==(const SDKNode &Other) const {
Children.size() == Other.Children.size()) {
for (unsigned I = 0; I < Children.size(); ++ I) {
if (*Children[I] != *Other.Children[I])
return Exit(false);
return false;
}
return Exit(true);
return true;
}
return Exit(false);
return false;
}
}
}
Expand Down