diff options
| author | River Riddle <riverriddle@google.com> | 2019-11-12 13:03:39 -0800 |
|---|---|---|
| committer | A. Unique TensorFlower <gardener@tensorflow.org> | 2019-11-12 13:04:14 -0800 |
| commit | 8082e3a687da76929e469ec76f86957c534f59db (patch) | |
| tree | 93ffa090b22d1768fa1e79b3c248f4507d2e398c /mlir/lib | |
| parent | 9d985141ef848f352c444cb950096c1e7ccadad9 (diff) | |
| download | bcm5719-llvm-8082e3a687da76929e469ec76f86957c534f59db.tar.gz bcm5719-llvm-8082e3a687da76929e469ec76f86957c534f59db.zip | |
NFC: Change DictionaryAttr::get(StringRef) to use binary search instead of a linear scan.
The elements of a DictionaryAttr are guaranteed to be sorted by name, so we can use a more efficient lookup when searching for an attribute.
PiperOrigin-RevId: 280035488
Diffstat (limited to 'mlir/lib')
| -rw-r--r-- | mlir/lib/IR/Attributes.cpp | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp index 80ac4a59246..5d7a4f08d1e 100644 --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -91,7 +91,7 @@ bool BoolAttr::getValue() const { return getImpl()->value; } /// NamedAttributes. static int compareNamedAttributes(const NamedAttribute *lhs, const NamedAttribute *rhs) { - return lhs->first.str().compare(rhs->first.str()); + return lhs->first.strref().compare(rhs->first.strref()); } DictionaryAttr DictionaryAttr::get(ArrayRef<NamedAttribute> value, @@ -155,10 +155,12 @@ ArrayRef<NamedAttribute> DictionaryAttr::getValue() const { /// Return the specified attribute if present, null otherwise. Attribute DictionaryAttr::get(StringRef name) const { - for (auto elt : getValue()) - if (elt.first.is(name)) - return elt.second; - return nullptr; + ArrayRef<NamedAttribute> values = getValue(); + auto compare = [](NamedAttribute attr, StringRef name) { + return attr.first.strref() < name; + }; + auto it = llvm::lower_bound(values, name, compare); + return it != values.end() && it->first.is(name) ? it->second : Attribute(); } Attribute DictionaryAttr::get(Identifier name) const { for (auto elt : getValue()) |

