[ADT] Consolidate uninitialized_copy in SmallVector (NFC)#161043
Merged
kazutakahirata merged 1 commit intoOct 1, 2025
Merged
Conversation
This patch consolidates two implementations of uninitialized_copy into a single template function.
Member
|
@llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) ChangesThis patch consolidates two implementations of uninitialized_copy into Full diff: https://github.com/llvm/llvm-project/pull/161043.diff 1 Files Affected:
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 36b324355ee10..8ccbaf8652193 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -501,25 +501,22 @@ class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
/// Copy the range [I, E) onto the uninitialized memory
/// starting with "Dest", constructing elements into it as needed.
- template<typename It1, typename It2>
+ template <typename It1, typename It2>
static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
- // Arbitrary iterator types; just use the basic implementation.
- std::uninitialized_copy(I, E, Dest);
- }
-
- /// Copy the range [I, E) onto the uninitialized memory
- /// starting with "Dest", constructing elements into it as needed.
- template <typename T1, typename T2>
- static void uninitialized_copy(
- T1 *I, T1 *E, T2 *Dest,
- std::enable_if_t<std::is_same<std::remove_const_t<T1>, T2>::value> * =
- nullptr) {
- // Use memcpy for PODs iterated by pointers (which includes SmallVector
- // iterators): std::uninitialized_copy optimizes to memmove, but we can
- // use memcpy here. Note that I and E are iterators and thus might be
- // invalid for memcpy if they are equal.
- if (I != E)
- std::memcpy(reinterpret_cast<void *>(Dest), I, (E - I) * sizeof(T));
+ if constexpr (std::is_pointer_v<It1> && std::is_pointer_v<It2> &&
+ std::is_same_v<
+ std::remove_const_t<std::remove_pointer_t<It1>>,
+ std::remove_pointer_t<It2>>) {
+ // Use memcpy for PODs iterated by pointers (which includes SmallVector
+ // iterators): std::uninitialized_copy optimizes to memmove, but we can
+ // use memcpy here. Note that I and E are iterators and thus might be
+ // invalid for memcpy if they are equal.
+ if (I != E)
+ std::memcpy(reinterpret_cast<void *>(Dest), I, (E - I) * sizeof(T));
+ } else {
+ // Arbitrary iterator types; just use the basic implementation.
+ std::uninitialized_copy(I, E, Dest);
+ }
}
/// Double the size of the allocated memory, guaranteeing space for at
|
tgymnich
approved these changes
Sep 28, 2025
kuhar
reviewed
Sep 28, 2025
kuhar
approved these changes
Sep 30, 2025
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/95/builds/18527 Here is the relevant piece of the build log for the reference |
mahesh-attarde
pushed a commit
to mahesh-attarde/llvm-project
that referenced
this pull request
Oct 3, 2025
This patch consolidates two implementations of uninitialized_copy into a single template function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patch consolidates two implementations of uninitialized_copy into
a single template function.