-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[WIP][ADT] Avoid slow size queries on append #136365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,10 +36,18 @@ template <typename T> class ArrayRef; | |
|
||
template <typename IteratorT> class iterator_range; | ||
|
||
template <class Iterator> | ||
using EnableIfConvertibleToInputIterator = std::enable_if_t<std::is_convertible< | ||
namespace detail { | ||
template <typename Iterator, typename IteratorCategory> | ||
inline constexpr bool IsOfIteratorCategory = std::is_convertible_v< | ||
typename std::iterator_traits<Iterator>::iterator_category, | ||
std::input_iterator_tag>::value>; | ||
IteratorCategory>; | ||
|
||
template <typename Iterator> | ||
using EnableIfConvertibleToInputIterator = | ||
std::enable_if_t<std::is_convertible_v< | ||
typename std::iterator_traits<Iterator>::iterator_category, | ||
std::input_iterator_tag>>; | ||
} // namespace detail | ||
|
||
/// This is all the stuff common to all SmallVectors. | ||
/// | ||
|
@@ -679,13 +687,37 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> { | |
void swap(SmallVectorImpl &RHS); | ||
|
||
/// Add the specified range to the end of the SmallVector. | ||
template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>> | ||
template <typename ItTy, | ||
typename = detail::EnableIfConvertibleToInputIterator<ItTy>> | ||
void append(ItTy in_start, ItTy in_end) { | ||
this->assertSafeToAddRange(in_start, in_end); | ||
size_type NumInputs = std::distance(in_start, in_end); | ||
this->reserve(this->size() + NumInputs); | ||
this->uninitialized_copy(in_start, in_end, this->end()); | ||
this->set_size(this->size() + NumInputs); | ||
if constexpr (detail::IsOfIteratorCategory< | ||
ItTy, std::random_access_iterator_tag>) { | ||
// Only reserve the required extra size upfront when the size calculation | ||
// is guaranteed to be O(1). | ||
size_type NumInputs = std::distance(in_start, in_end); | ||
this->reserve(this->size() + NumInputs); | ||
this->uninitialized_copy(in_start, in_end, this->end()); | ||
this->set_size(this->size() + NumInputs); | ||
} else { | ||
// Otherwise, append using `in_end` as the sentinel and reserve more space | ||
// as necessary. | ||
for (ItTy It = in_start; It != in_end;) { | ||
iterator Dest = this->end(); | ||
size_type InitialSize = this->size(); | ||
size_type ExtraCap = this->capacity() - InitialSize; | ||
size_type NumCopied = 0; | ||
for (; NumCopied != ExtraCap && It != in_end; ++It, ++NumCopied) | ||
::new ((void *)(Dest + NumCopied)) T(*It); | ||
|
||
size_type NewSize = InitialSize + NumCopied; | ||
this->set_size(NewSize); | ||
|
||
if (It != in_end) { | ||
this->reserve(NewSize + 1); | ||
} | ||
kuhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
/// Append \p NumInputs copies of \p Elt to the end. | ||
|
@@ -720,7 +752,8 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> { | |
// FIXME: Consider assigning over existing elements, rather than clearing & | ||
// re-initializing them - for all assign(...) variants. | ||
|
||
template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>> | ||
template <typename ItTy, | ||
typename = detail::EnableIfConvertibleToInputIterator<ItTy>> | ||
void assign(ItTy in_start, ItTy in_end) { | ||
this->assertSafeToReferenceAfterClear(in_start, in_end); | ||
clear(); | ||
|
@@ -871,12 +904,14 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> { | |
return I; | ||
} | ||
|
||
template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>> | ||
template <typename ItTy, | ||
typename = detail::EnableIfConvertibleToInputIterator<ItTy>> | ||
iterator insert(iterator I, ItTy From, ItTy To) { | ||
// Convert iterator to elt# to avoid invalidating iterator when we reserve() | ||
size_t InsertElt = I - this->begin(); | ||
|
||
if (I == this->end()) { // Important special case for empty vector. | ||
// Important special case for appending to a vector, including empty vector. | ||
if (I == this->end()) { | ||
append(From, To); | ||
return this->begin()+InsertElt; | ||
} | ||
|
@@ -887,8 +922,8 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> { | |
this->assertSafeToAddRange(From, To); | ||
|
||
size_t NumToInsert = std::distance(From, To); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you going to address this call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only invoked when we insert in the middle of the vector -- I'm not sure how much we care about this. We could potentially first append and then when we know size do a rotate, but I'm not sure it's worth it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I misread the code... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is what the |
||
|
||
// Ensure there is enough space. | ||
// Ensure there is enough space so that we do not have to re-allocate mid | ||
// insertion. | ||
reserve(this->size() + NumToInsert); | ||
|
||
// Uninvalidate the iterator. | ||
|
@@ -1212,7 +1247,8 @@ class LLVM_GSL_OWNER SmallVector : public SmallVectorImpl<T>, | |
this->assign(Size, Value); | ||
} | ||
|
||
template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>> | ||
template <typename ItTy, | ||
typename = detail::EnableIfConvertibleToInputIterator<ItTy>> | ||
SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) { | ||
this->append(S, E); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use the newly introduced
IsOfIteratorCategory
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no because of SFINAE weirdness...