From 42b872688a73fa60e98a602e92991828a27027bf Mon Sep 17 00:00:00 2001 From: River Riddle Date: Fri, 1 May 2020 11:56:21 -0700 Subject: [PATCH] [IndexedAccessorRange] Only offset the base if the index is non-zero. This is more efficient and removes the need for derived ranges to handle the degenerate empty case. --- include/llvm/ADT/STLExtras.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index 595ecbd0729..30bcdf5268a 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -1108,7 +1108,7 @@ public: }; indexed_accessor_range_base(iterator begin, iterator end) - : base(DerivedT::offset_base(begin.getBase(), begin.getIndex())), + : base(offset_base(begin.getBase(), begin.getIndex())), count(end.getIndex() - begin.getIndex()) {} indexed_accessor_range_base(const iterator_range &range) : indexed_accessor_range_base(range.begin(), range.end()) {} @@ -1141,7 +1141,7 @@ public: /// Drop the first N elements, and keep M elements. DerivedT slice(size_t n, size_t m) const { assert(n + m <= size() && "invalid size specifiers"); - return DerivedT(DerivedT::offset_base(base, n), m); + return DerivedT(offset_base(base, n), m); } /// Drop the first n elements. @@ -1174,6 +1174,12 @@ public: return RangeT(iterator_range(*this)); } +private: + /// Offset the given base by the given amount. + static BaseT offset_base(const BaseT &base, size_t n) { + return n == 0 ? base : DerivedT::offset_base(base, n); + } + protected: indexed_accessor_range_base(const indexed_accessor_range_base &) = default; indexed_accessor_range_base(indexed_accessor_range_base &&) = default;