| 174 | } |
| 175 | |
| 176 | const AttrValue* AttrSlice::Find(StringPiece attr_name) const { |
| 177 | // Currently, the collection used for NodeDef::attr() (google::protobuf::Map) |
| 178 | // requires that the keys used for lookups have type 'const string&'. Because |
| 179 | // this method takes a StringPiece, it is necessary to allocate a temporary |
| 180 | // string, copy attr_name to it, and then use that temporary string for the |
| 181 | // lookup. This causes an excessive number of short-lived allocations, and for |
| 182 | // large graphs, this can be a significant cost. |
| 183 | // |
| 184 | // Because most nodes have a small number of attributes, a simple linear scan |
| 185 | // is generally more efficient than a hashed lookup. If google::protobuf::Map |
| 186 | // changes so that it supports efficient lookups using StringPiece instead of |
| 187 | // const string&, then this code could be changed to use attrs_->find() again. |
| 188 | |
| 189 | for (const auto& attr : *attrs_) { |
| 190 | if (attr.first == attr_name) { |
| 191 | return &attr.second; |
| 192 | } |
| 193 | } |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
| 197 | Status AttrSlice::Find(StringPiece attr_name, |
| 198 | const AttrValue** attr_value) const { |
no test coverage detected