Skip to content

Commit

Permalink
Merge pull request godotengine#100477 from Ivorforce/cowdata-move-ins…
Browse files Browse the repository at this point in the history
…ert-n-remove

Optimize `CowData` and `LocalVector` functions `.insert` and `.remove_at` by using move semantics.
  • Loading branch information
Repiteo committed Dec 20, 2024
2 parents 1b8a2d9 + a636c04 commit 151e7fc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions core/templates/cowdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <string.h>
#include <initializer_list>
#include <type_traits>
#include <utility>

template <typename T>
class Vector;
Expand Down Expand Up @@ -226,7 +227,7 @@ class CowData {
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);
Expand All @@ -239,7 +240,7 @@ class CowData {
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;

Expand Down
12 changes: 6 additions & 6 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ class LocalVector {
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);
}
}

void remove_at(U p_index) {
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();
Expand All @@ -88,7 +88,7 @@ class LocalVector {
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();
Expand Down Expand Up @@ -245,13 +245,13 @@ class LocalVector {
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);
}
}

Expand Down

0 comments on commit 151e7fc

Please sign in to comment.