From 48a4c9c1adb6c5b3e0e602d3d8214291eea6b429 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 3 Nov 2022 10:33:52 +0330 Subject: [PATCH] AK: Use TypedTransfer to move vector's inline buffer This avoids an explicit loop-move when the type is trivial. --- AK/TypedTransfer.h | 17 +++++++++++++++-- AK/Vector.h | 6 ++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/AK/TypedTransfer.h b/AK/TypedTransfer.h index 64ed7fb2748..80625b0f3c4 100644 --- a/AK/TypedTransfer.h +++ b/AK/TypedTransfer.h @@ -31,7 +31,7 @@ public: } } - static size_t copy(T* destination, const T* source, size_t count) + static size_t copy(T* destination, T const* source, size_t count) { if (count == 0) return 0; @@ -54,7 +54,7 @@ public: return count; } - static bool compare(const T* a, const T* b, size_t count) + static bool compare(T const* a, T const* b, size_t count) { if (count == 0) return true; @@ -69,6 +69,19 @@ public: return true; } + + static void delete_(T* ptr, size_t count) + { + if (count == 0) + return; + + if constexpr (Traits::is_trivial()) { + return; + } + + for (size_t i = 0; i < count; ++i) + ptr[i].~T(); + } }; } diff --git a/AK/Vector.h b/AK/Vector.h index c56761e369a..1b4c4713fa5 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -73,10 +73,8 @@ public: { if constexpr (inline_capacity > 0) { if (!m_outline_buffer) { - for (size_t i = 0; i < m_size; ++i) { - new (&inline_buffer()[i]) StorageType(move(other.inline_buffer()[i])); - other.inline_buffer()[i].~StorageType(); - } + TypedTransfer::move(inline_buffer(), other.inline_buffer(), m_size); + TypedTransfer::delete_(other.inline_buffer(), m_size); } } other.m_outline_buffer = nullptr;