[llvm] 4d04526 - ADT: Avoid using SmallVector::set_size() in SmallString
Duncan P. N. Exon Smith via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 11 17:38:07 PST 2022
Author: Duncan P. N. Exon Smith
Date: 2022-01-11T17:33:13-08:00
New Revision: 4d04526bf48d982055fd625803734b2afded3afb
URL: https://github.com/llvm/llvm-project/commit/4d04526bf48d982055fd625803734b2afded3afb
DIFF: https://github.com/llvm/llvm-project/commit/4d04526bf48d982055fd625803734b2afded3afb.diff
LOG: ADT: Avoid using SmallVector::set_size() in SmallString
Update `SmallString::append()` to use `resize_for_overwrite()` instead
of `reserve()` followed by `set_size()`.
Differential Revision: https://reviews.llvm.org/D115382
Added:
Modified:
llvm/include/llvm/ADT/SmallString.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/SmallString.h b/llvm/include/llvm/ADT/SmallString.h
index 56b0639b98cc3..81243af1f97d2 100644
--- a/llvm/include/llvm/ADT/SmallString.h
+++ b/llvm/include/llvm/ADT/SmallString.h
@@ -70,16 +70,16 @@ class SmallString : public SmallVector<char, InternalLen> {
/// Append from a list of StringRefs.
void append(std::initializer_list<StringRef> Refs) {
- size_t SizeNeeded = this->size();
+ size_t CurrentSize = this->size();
+ size_t SizeNeeded = CurrentSize;
for (const StringRef &Ref : Refs)
SizeNeeded += Ref.size();
- this->reserve(SizeNeeded);
- auto CurEnd = this->end();
+ this->resize_for_overwrite(SizeNeeded);
for (const StringRef &Ref : Refs) {
- this->uninitialized_copy(Ref.begin(), Ref.end(), CurEnd);
- CurEnd += Ref.size();
+ std::copy(Ref.begin(), Ref.end(), this->begin() + CurrentSize);
+ CurrentSize += Ref.size();
}
- this->set_size(SizeNeeded);
+ assert(CurrentSize == this->size());
}
/// @}
More information about the llvm-commits
mailing list