Skip to content

Commit 0e17eae

Browse files
Merge pull request #360 from correctcomputation/optimize_graph_insert
Optimize node insertion method for graph class
2 parents 6531b2e + 8ce05dc commit 0e17eae

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

clang/include/clang/3C/ConstraintsGraph.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ class DataGraph
121121
}
122122

123123
void removeEdge(Data Src, Data Dst) {
124-
auto *NSrc = this->findNode(NodeType(Src));
125-
auto *NDst = this->findNode(NodeType(Dst));
126-
assert(NSrc != this->end() && NDst != this->end());
124+
NodeType *NSrc = this->findNode(Src);
125+
NodeType *NDst = this->findNode(Dst);
126+
assert(NSrc && NDst);
127127
llvm::SmallVector<EdgeType *, 10> Edges;
128-
(*NDst)->findEdgesTo(**NSrc, Edges);
128+
NDst->findEdgesTo(*NSrc, Edges);
129129
for (EdgeType *E : Edges) {
130-
(*NDst)->removeEdge(*E);
130+
NDst->removeEdge(*E);
131131
delete E;
132132
}
133133
invalidateBFSCache();
@@ -151,15 +151,15 @@ class DataGraph
151151
}
152152

153153
bool getNeighbors(Data D, std::set<Data> &DataSet, bool Succ) {
154-
auto *N = this->findNode(NodeType(D));
155-
if (N == this->end())
154+
NodeType *N = this->findNode(D);
155+
if (N == nullptr)
156156
return false;
157157
DataSet.clear();
158158
llvm::SetVector<EdgeType *> Edges;
159159
if (Succ)
160-
Edges = (*N)->getEdges();
160+
Edges = N->getEdges();
161161
else
162-
Edges = (*N)->getPredecessors();
162+
Edges = N->getPredecessors();
163163
for (auto *E : Edges)
164164
DataSet.insert(E->getTargetNode().getData());
165165
return !DataSet.empty();
@@ -173,14 +173,20 @@ class DataGraph
173173
return getNeighbors(D, DataSet, false);
174174
}
175175

176+
NodeType *findNode(Data D) {
177+
if (NodeSet.find(D) != NodeSet.end())
178+
return NodeSet[D];
179+
return nullptr;
180+
}
181+
176182
void visitBreadthFirst(Data Start, llvm::function_ref<void(Data)> Fn) {
177-
auto *N = this->findNode(NodeType(Start));
178-
if (N == this->end())
183+
NodeType *N = this->findNode(Start);
184+
if (N == nullptr)
179185
return;
180186
// Insert into BFS cache.
181187
if (BFSCache.find(Start) == BFSCache.end()) {
182188
std::set<Data> ReachableNodes;
183-
for (auto TNode : llvm::breadth_first(*N)) {
189+
for (auto TNode : llvm::breadth_first(N)) {
184190
ReachableNodes.insert(TNode->getData());
185191
}
186192
BFSCache[Start] = ReachableNodes;
@@ -194,19 +200,20 @@ class DataGraph
194200
// is allocated. Node equality is defined only by the data stored in a node,
195201
// so if any node already contains the data, this node will be found.
196202
virtual NodeType *findOrCreateNode(Data D) {
197-
auto *OldN = this->findNode(NodeType(D));
198-
if (OldN != this->end())
199-
return *OldN;
203+
if (NodeSet.find(D) != NodeSet.end())
204+
return NodeSet[D];
200205

201206
auto *NewN = new NodeType(D);
202-
this->addNode(*NewN);
207+
this->Nodes.push_back(NewN);
208+
NodeSet[D] = NewN;
203209
return NewN;
204210
}
205211

206212
private:
207213
template <typename G> friend struct llvm::GraphTraits;
208214
friend class GraphVizOutputGraph;
209215
std::map<Data, std::set<Data>> BFSCache;
216+
std::map<Data, NodeType*> NodeSet;
210217

211218
void invalidateBFSCache() { BFSCache.clear(); }
212219
};

0 commit comments

Comments
 (0)