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
This commit is contained in:
A. Unique TensorFlower 2020-04-06 10:52:20 -07:00 committed by TensorFlower Gardener
parent 111b0c8401
commit 0755bc921a
3 changed files with 5 additions and 5 deletions

View File

@ -125,7 +125,7 @@ NodeDef* AddNode(StringPiece name, StringPiece op,
for (const string& input : inputs) { for (const string& input : inputs) {
node->add_input(input); node->add_input(input);
} }
for (auto attr : attributes) { for (const auto& attr : attributes) {
(*node->mutable_attr())[attr.first] = attr.second; (*node->mutable_attr())[attr.first] = attr.second;
} }
return node; return node;

View File

@ -114,7 +114,7 @@ NodeDef* AddNode(StringPiece name, StringPiece op,
for (const string& input : inputs) { for (const string& input : inputs) {
node.add_input(input); node.add_input(input);
} }
for (auto attr : attributes) { for (const auto& attr : attributes) {
(*node.mutable_attr())[attr.first] = attr.second; (*node.mutable_attr())[attr.first] = attr.second;
} }
return graph->AddNode(std::move(node)); return graph->AddNode(std::move(node));

View File

@ -207,12 +207,12 @@ absl::flat_hash_map<string, absl::flat_hash_set<int>> IdentityNTerminalPorts(
// get pruned later on. // get pruned later on.
absl::flat_hash_set<string> visited(terminal_nodes.begin(), absl::flat_hash_set<string> visited(terminal_nodes.begin(),
terminal_nodes.end()); terminal_nodes.end());
for (string terminal_node : terminal_nodes) { for (const string& terminal_node : terminal_nodes) {
NodeDef* node = node_map.GetNode(terminal_node); NodeDef* node = node_map.GetNode(terminal_node);
if (node == nullptr) { if (node == nullptr) {
continue; continue;
} }
for (string input : node->input()) { for (const string& input : node->input()) {
to_visit.push_back(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); node_map->AddOutput(update.input, update.output);
} }