Skip to content

Commit 7f52de1

Browse files
asimshankartensorflower-gardener
authored andcommitted
Make Graph::UpdateEdge() be O(e) instead of O(E)
where: - E = number of edges in the graph - e = number of edges on the node of interest e is necessarily <= E and is typically really small (# of inputs to an operation + control edges) PiperOrigin-RevId: 210624296
1 parent bf7b20e commit 7f52de1

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

tensorflow/core/graph/graph.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,15 @@ void Graph::RemoveControlEdge(const Edge* e) {
495495
RemoveEdge(e);
496496
}
497497

498+
namespace {
499+
const Edge* FindEdge(const Node* dst, int index) {
500+
for (const Edge* e : dst->in_edges()) {
501+
if (e->dst_input() == index) return e;
502+
}
503+
return nullptr;
504+
}
505+
} // namespace
506+
498507
Status Graph::UpdateEdge(Node* new_src, int new_src_index, Node* dst,
499508
int dst_index) {
500509
TF_RETURN_IF_ERROR(IsValidOutputTensor(new_src, new_src_index));
@@ -512,17 +521,6 @@ Status Graph::UpdateEdge(Node* new_src, int new_src_index, Node* dst,
512521
return Status::OK();
513522
}
514523

515-
const Edge* Graph::FindEdge(const Node* dst, int index) {
516-
for (const Edge* e : edges_) {
517-
// edges_ will contain null edges if RemoveEdge() was called.
518-
if (e == nullptr) continue;
519-
if (e->dst() == dst && e->dst_input() == index) {
520-
return e;
521-
}
522-
}
523-
return nullptr;
524-
}
525-
526524
Status Graph::AddFunctionLibrary(const FunctionDefLibrary& fdef_lib) {
527525
// Need a new-enough consumer to support the functions we add to the graph.
528526
if (fdef_lib.function_size() > 0 && versions_->min_consumer() < 12) {

tensorflow/core/graph/graph.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,6 @@ class Graph {
680680
// AddWhileContext() or Node::while_ctx(), but this manages the lifetime.
681681
std::map<string, WhileContext> while_ctxs_;
682682

683-
// Searches through edges_ for the Edge whose destination node and index
684-
// matches dst. An edge with destination `dst` must exist in the graph.
685-
const Edge* FindEdge(const Node* dst, int index);
686-
687683
TF_DISALLOW_COPY_AND_ASSIGN(Graph);
688684
};
689685

0 commit comments

Comments
 (0)