Skip to content

Commit fa6096c

Browse files
committed
[WTF] Fix performance when creating Vectors from different constness
https://bugs.webkit.org/show_bug.cgi?id=299008 Reviewed by Chris Dumez. For example, when you try to create a Vector<uint8_t> from a span<const uint8_t>, templates will consider both types different and even when you pass true to the template, the second VectorCopier::uninitializedCopy function will be used and will defer to the slow version. My use case was in MSE, creating a SharedBuffer implies creating a FragmentedSharedBuffer and then a DataSegment that implicitly creates a Vector<uint8_t> from a span<const uint8_t>. If you're copying 2.5MB, time is reduced to 33% of the initial one. * Source/WTF/wtf/Vector.h: Canonical link: https://commits.webkit.org/300464@main
1 parent 8774900 commit fa6096c

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Source/WTF/wtf/Vector.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,14 @@ struct VectorCopier<false, T>
185185
template<typename T>
186186
struct VectorCopier<true, T>
187187
{
188-
static void uninitializedCopy(const T* src, const T* srcEnd, T* dst)
188+
static void uninitializedCopy(const T* src, const T* srcEnd, std::remove_const_t<T>* dst)
189189
{
190-
memcpy(static_cast<void*>(dst), static_cast<void*>(const_cast<T*>(src)), reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
190+
memcpy(static_cast<void*>(dst), static_cast<void*>(const_cast<std::remove_const_t<T>*>(src)), reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
191191
}
192192
template<typename U>
193193
static void uninitializedCopy(const T* src, const T* srcEnd, U* dst)
194194
{
195+
static_assert(!std::is_same_v<std::remove_const_t<T>, std::remove_const_t<U>>, "We should be using the faster overload for this, which uses memcpy()");
195196
VectorCopier<false, T>::uninitializedCopy(src, srcEnd, dst);
196197
}
197198
};

0 commit comments

Comments
 (0)