Skip to content

Commit 28aa6d4

Browse files
committed
Matrix: an optimization to cherry-pick
1 parent 9e43ada commit 28aa6d4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

llvm/include/llvm/ADT/Matrix.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct MatrixStorageBase : public SmallVectorImpl<T>, SmallVectorStorage<T, N> {
3636
using SmallVectorImpl<T>::append;
3737
using SmallVectorImpl<T>::erase;
3838
using SmallVectorImpl<T>::destroy_range;
39+
using SmallVectorImpl<T>::isSafeToReferenceAfterResize;
3940

4041
T *begin() const { return const_cast<T *>(SmallVectorImpl<T>::begin()); }
4142
T *end() const { return const_cast<T *>(SmallVectorImpl<T>::end()); }
@@ -97,6 +98,10 @@ class MatrixStorage {
9798
Base.pop_back_n(NCols);
9899
}
99100

101+
bool willReallocateOnAddRow() const {
102+
return Base.capacity() < Base.size() + NCols;
103+
}
104+
100105
private:
101106
MatrixStorageBase<T, N> Base;
102107
size_t NCols;
@@ -239,6 +244,13 @@ class [[nodiscard]] JaggedArrayView {
239244
}
240245

241246
void addRow(const SmallVectorImpl<T> &Row) {
247+
// Optimization when we know that the underying storage won't be resized.
248+
if (!Mat.willReallocateOnAddRow()) {
249+
Mat.addRow(Row);
250+
RowView.emplace_back(Mat.rowFromIdx(Mat.getNumRows() - 1), Row.size());
251+
return;
252+
}
253+
242254
// The underlying storage may be resized, performing reallocations. The
243255
// pointers in RowView will no longer be valid, so save and restore the
244256
// data. Construct RestoreData by performing pointer-arithmetic on the

0 commit comments

Comments
 (0)