From c0cce748cc094f8d7a32b744f8d65ab351e2bb86 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 15 Jul 2025 00:12:31 +0200 Subject: [PATCH] Optimize `Array::RemoveAtKeepOrder` --- Source/Engine/Core/Collections/Array.h | 8 +--- Source/Engine/Core/Memory/Memory.h | 63 ++++++++------------------ 2 files changed, 19 insertions(+), 52 deletions(-) diff --git a/Source/Engine/Core/Collections/Array.h b/Source/Engine/Core/Collections/Array.h index 7cb4a3248..4f660d2a9 100644 --- a/Source/Engine/Core/Collections/Array.h +++ b/Source/Engine/Core/Collections/Array.h @@ -658,13 +658,7 @@ public: --_count; T* data = _allocation.Get(); if (index < _count) - { - T* dst = data + index; - T* src = data + (index + 1); - const int32 count = _count - index; - for (int32 i = 0; i < count; ++i) - dst[i] = MoveTemp(src[i]); - } + Memory::MoveAssignItems(data + index, data + (index + 1), _count - index); Memory::DestructItems(data + _count, 1); } diff --git a/Source/Engine/Core/Memory/Memory.h b/Source/Engine/Core/Memory/Memory.h index 329ee372d..772c9fb48 100644 --- a/Source/Engine/Core/Memory/Memory.h +++ b/Source/Engine/Core/Memory/Memory.h @@ -104,12 +104,6 @@ public: { new(dst) T(); } - - /// - /// Constructs the item in the memory. - /// - /// The optimized version is noop. - /// The address of the memory location to construct. template FORCE_INLINE static typename TEnableIf::Value>::Type ConstructItem(T* dst) { @@ -132,13 +126,6 @@ public: ++(T*&)dst; } } - - /// - /// Constructs the range of items in the memory. - /// - /// The optimized version is noop. - /// The address of the first memory location to construct. - /// The number of element to construct. Can be equal 0. template FORCE_INLINE static typename TEnableIf::Value>::Type ConstructItems(T* dst, int32 count) { @@ -163,14 +150,6 @@ public: ++src; } } - - /// - /// Constructs the range of items in the memory from the set of arguments. - /// - /// The optimized version uses low-level memory copy. - /// The address of the first memory location to construct. - /// The address of the first memory location to pass to the constructor. - /// The number of element to construct. Can be equal 0. template FORCE_INLINE static typename TEnableIf::Value>::Type ConstructItems(T* dst, const U* src, int32 count) { @@ -187,12 +166,6 @@ public: { dst->~T(); } - - /// - /// Destructs the item in the memory. - /// - /// The optimized version is noop. - /// The address of the memory location to destruct. template FORCE_INLINE static typename TEnableIf::Value>::Type DestructItem(T* dst) { @@ -213,13 +186,6 @@ public: ++dst; } } - - /// - /// Destructs the range of items in the memory. - /// - /// The optimized version is noop. - /// The address of the first memory location to destruct. - /// The number of element to destruct. Can be equal 0. template FORCE_INLINE static typename TEnableIf::Value>::Type DestructItems(T* dst, int32 count) { @@ -242,15 +208,7 @@ public: ++src; } } - - /// - /// Copies the range of items using the assignment operator. - /// - /// The optimized version is low-level memory copy. - /// The address of the first memory location to start assigning to. - /// The address of the first memory location to assign from. - /// The number of element to assign. Can be equal 0. - template + template FORCE_INLINE static typename TEnableIf::Value>::Type CopyItems(T* dst, const T* src, int32 count) { Platform::MemoryCopy(dst, src, count * sizeof(T)); @@ -273,16 +231,31 @@ public: ++src; } } + template + FORCE_INLINE static typename TEnableIf::Value>::Type MoveItems(T* dst, U* src, int32 count) + { + Platform::MemoryCopy(dst, src, count * sizeof(U)); + } /// - /// Moves the range of items in the memory from the set of arguments. + /// Moves the range of items using the assignment operator. /// /// The optimized version uses low-level memory copy. /// The address of the first memory location to move. /// The address of the first memory location to pass to the move constructor. /// The number of element to move. Can be equal 0. template - FORCE_INLINE static typename TEnableIf::Value>::Type MoveItems(T* dst, U* src, int32 count) + FORCE_INLINE static typename TEnableIf::Value>::Type MoveAssignItems(T* dst, U* src, int32 count) + { + while (count--) + { + *dst = MoveTemp(*src); + ++(T*&)dst; + ++src; + } + } + template + FORCE_INLINE static typename TEnableIf::Value>::Type MoveAssignItems(T* dst, U* src, int32 count) { Platform::MemoryCopy(dst, src, count * sizeof(U)); }