Merge pull request #100477 from Ivorforce/cowdata-move-insert-n-remove

Optimize `CowData` and `LocalVector` functions `.insert` and `.remove_at` by using move semantics.
This commit is contained in:
Thaddeus Crews 2024-12-19 20:00:20 -06:00
commit 151e7fc687
No known key found for this signature in database
GPG Key ID: 62181B86FE9E5D84
2 changed files with 9 additions and 8 deletions

View File

@ -38,6 +38,7 @@
#include <string.h>
#include <initializer_list>
#include <type_traits>
#include <utility>
template <typename T>
class Vector;
@ -226,7 +227,7 @@ public:
T *p = ptrw();
Size len = size();
for (Size i = p_index; i < len - 1; i++) {
p[i] = p[i + 1];
p[i] = std::move(p[i + 1]);
}
resize(len - 1);
@ -239,7 +240,7 @@ public:
ERR_FAIL_COND_V(err, err);
T *p = ptrw();
for (Size i = new_size - 1; i > p_pos; i--) {
p[i] = p[i - 1];
p[i] = std::move(p[i - 1]);
}
p[p_pos] = p_val;

View File

@ -67,7 +67,7 @@ public:
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
memnew_placement(&data[count++], T(p_elem));
} else {
data[count++] = p_elem;
data[count++] = std::move(p_elem);
}
}
@ -75,7 +75,7 @@ public:
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
count--;
for (U i = p_index; i < count; i++) {
data[i] = data[i + 1];
data[i] = std::move(data[i + 1]);
}
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
data[count].~T();
@ -88,7 +88,7 @@ public:
ERR_FAIL_INDEX(p_index, count);
count--;
if (count > p_index) {
data[p_index] = data[count];
data[p_index] = std::move(data[count]);
}
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
data[count].~T();
@ -245,13 +245,13 @@ public:
void insert(U p_pos, T p_val) {
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
if (p_pos == count) {
push_back(p_val);
push_back(std::move(p_val));
} else {
resize(count + 1);
for (U i = count - 1; i > p_pos; i--) {
data[i] = data[i - 1];
data[i] = std::move(data[i - 1]);
}
data[p_pos] = p_val;
data[p_pos] = std::move(p_val);
}
}