@@ -808,17 +808,45 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
808
808
return Result;
809
809
}
810
810
811
+ // / This is for caching the comparison results between two SDKNodes.
812
+ class SDKNodeEqualContext {
813
+ using NodePtrAndEqual = llvm::DenseMap<const SDKNode*, bool >;
814
+ llvm::DenseMap<const SDKNode*, llvm::DenseMap<const SDKNode*, bool >> Data;
815
+
816
+ public:
817
+ Optional<bool > getEquality (const SDKNode* Left, const SDKNode* Right) {
818
+ auto &Map = Data.insert ({Left, NodePtrAndEqual ()}).first ->getSecond ();
819
+ if (Map.count (Right))
820
+ return Map[Right];
821
+ return None;
822
+ }
823
+
824
+ void addEquality (const SDKNode* Left, const SDKNode* Right, const bool Value) {
825
+ Data.insert (std::make_pair (Left, NodePtrAndEqual ())).first ->getSecond ().
826
+ insert ({Right, Value});
827
+ }
828
+ };
829
+
811
830
bool SDKNode::operator ==(const SDKNode &Other) const {
831
+ static SDKNodeEqualContext EqualCache;
832
+ if (auto Cached = EqualCache.getEquality (this , &Other)) {
833
+ return Cached.getValue ();
834
+ }
835
+ auto Exit = [&](const bool Result) {
836
+ EqualCache.addEquality (this , &Other, Result);
837
+ return Result;
838
+ };
839
+
812
840
if (getKind () != Other.getKind ())
813
- return false ;
841
+ return Exit ( false ) ;
814
842
815
843
switch (getKind ()) {
816
844
case SDKNodeKind::TypeNominal:
817
845
case SDKNodeKind::TypeFunc: {
818
846
auto Left = this ->getAs <SDKNodeType>();
819
847
auto Right = (&Other)->getAs <SDKNodeType>();
820
- return Left->getTypeAttributes ().equals (Right->getTypeAttributes ())
821
- && Left->getPrintedName () == Right->getPrintedName ();
848
+ return Exit ( Left->getTypeAttributes ().equals (Right->getTypeAttributes ())
849
+ && Left->getPrintedName () == Right->getPrintedName ()) ;
822
850
}
823
851
824
852
case SDKNodeKind::Function:
@@ -828,9 +856,9 @@ bool SDKNode::operator==(const SDKNode &Other) const {
828
856
auto Left = this ->getAs <SDKNodeAbstractFunc>();
829
857
auto Right = (&Other)->getAs <SDKNodeAbstractFunc>();
830
858
if (Left->isMutating () ^ Right->isMutating ())
831
- return false ;
859
+ return Exit ( false ) ;
832
860
if (Left->isThrowing () ^ Right->isThrowing ())
833
- return false ;
861
+ return Exit ( false ) ;
834
862
SWIFT_FALLTHROUGH;
835
863
}
836
864
case SDKNodeKind::TypeDecl:
@@ -842,11 +870,11 @@ bool SDKNode::operator==(const SDKNode &Other) const {
842
870
Children.size () == Other.Children .size ()) {
843
871
for (unsigned I = 0 ; I < Children.size (); ++ I) {
844
872
if (*Children[I] != *Other.Children [I])
845
- return false ;
873
+ return Exit ( false ) ;
846
874
}
847
- return true ;
875
+ return Exit ( true ) ;
848
876
}
849
- return false ;
877
+ return Exit ( false ) ;
850
878
}
851
879
}
852
880
}
0 commit comments