From 0755bc921a144f7acbabec0a7f7ae502e5b48ff5 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Mon, 6 Apr 2020 10:52:20 -0700 Subject: [PATCH] This CL optimizes C++11 range-based for loops where the variable is copied in each iteration but it would suffice to obtain it by const reference. This is only applied to loop variables of types that are expensive to copy which means they are not trivially copyable or have a non-trivial copy constructor or destructor. To ensure that it is safe to replace the copy with a const reference the following heuristic is employed: The loop variable is const qualified. The loop variable is not const, but only const methods or operators are invoked on it, or it is used as const reference or value argument in constructors or function calls. PiperOrigin-RevId: 305067323 Change-Id: I7e170e37b721aa25870a422dcfa2e3052adf3557 --- tensorflow/core/grappler/optimizers/data/function_utils.cc | 2 +- tensorflow/core/grappler/optimizers/data/graph_utils.cc | 2 +- tensorflow/core/grappler/optimizers/model_pruner.cc | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow/core/grappler/optimizers/data/function_utils.cc b/tensorflow/core/grappler/optimizers/data/function_utils.cc index 40f4f24b03f..cd37887ec94 100644 --- a/tensorflow/core/grappler/optimizers/data/function_utils.cc +++ b/tensorflow/core/grappler/optimizers/data/function_utils.cc @@ -125,7 +125,7 @@ NodeDef* AddNode(StringPiece name, StringPiece op, for (const string& input : inputs) { node->add_input(input); } - for (auto attr : attributes) { + for (const auto& attr : attributes) { (*node->mutable_attr())[attr.first] = attr.second; } return node; diff --git a/tensorflow/core/grappler/optimizers/data/graph_utils.cc b/tensorflow/core/grappler/optimizers/data/graph_utils.cc index ce56b7c3b0e..8bc33ea8464 100644 --- a/tensorflow/core/grappler/optimizers/data/graph_utils.cc +++ b/tensorflow/core/grappler/optimizers/data/graph_utils.cc @@ -114,7 +114,7 @@ NodeDef* AddNode(StringPiece name, StringPiece op, for (const string& input : inputs) { node.add_input(input); } - for (auto attr : attributes) { + for (const auto& attr : attributes) { (*node.mutable_attr())[attr.first] = attr.second; } return graph->AddNode(std::move(node)); diff --git a/tensorflow/core/grappler/optimizers/model_pruner.cc b/tensorflow/core/grappler/optimizers/model_pruner.cc index 3c473b23f6e..243ab7bd965 100644 --- a/tensorflow/core/grappler/optimizers/model_pruner.cc +++ b/tensorflow/core/grappler/optimizers/model_pruner.cc @@ -207,12 +207,12 @@ absl::flat_hash_map> IdentityNTerminalPorts( // get pruned later on. absl::flat_hash_set visited(terminal_nodes.begin(), terminal_nodes.end()); - for (string terminal_node : terminal_nodes) { + for (const string& terminal_node : terminal_nodes) { NodeDef* node = node_map.GetNode(terminal_node); if (node == nullptr) { continue; } - for (string input : node->input()) { + for (const string& input : node->input()) { to_visit.push_back(input); } } @@ -353,7 +353,7 @@ Status RewriteIdentityNAndInputsOutputs( } } - for (NodeOutputUpdate update : updates) { + for (const NodeOutputUpdate& update : updates) { node_map->AddOutput(update.input, update.output); }