Skip to content

Commit 151e7fc

Browse files
committed
Merge pull request #100477 from Ivorforce/cowdata-move-insert-n-remove
Optimize `CowData` and `LocalVector` functions `.insert` and `.remove_at` by using move semantics.
2 parents 1b8a2d9 + a636c04 commit 151e7fc

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

core/templates/cowdata.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <string.h>
3939
#include <initializer_list>
4040
#include <type_traits>
41+
#include <utility>
4142

4243
template <typename T>
4344
class Vector;
@@ -226,7 +227,7 @@ class CowData {
226227
T *p = ptrw();
227228
Size len = size();
228229
for (Size i = p_index; i < len - 1; i++) {
229-
p[i] = p[i + 1];
230+
p[i] = std::move(p[i + 1]);
230231
}
231232

232233
resize(len - 1);
@@ -239,7 +240,7 @@ class CowData {
239240
ERR_FAIL_COND_V(err, err);
240241
T *p = ptrw();
241242
for (Size i = new_size - 1; i > p_pos; i--) {
242-
p[i] = p[i - 1];
243+
p[i] = std::move(p[i - 1]);
243244
}
244245
p[p_pos] = p_val;
245246

core/templates/local_vector.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ class LocalVector {
6767
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
6868
memnew_placement(&data[count++], T(p_elem));
6969
} else {
70-
data[count++] = p_elem;
70+
data[count++] = std::move(p_elem);
7171
}
7272
}
7373

7474
void remove_at(U p_index) {
7575
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
7676
count--;
7777
for (U i = p_index; i < count; i++) {
78-
data[i] = data[i + 1];
78+
data[i] = std::move(data[i + 1]);
7979
}
8080
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
8181
data[count].~T();
@@ -88,7 +88,7 @@ class LocalVector {
8888
ERR_FAIL_INDEX(p_index, count);
8989
count--;
9090
if (count > p_index) {
91-
data[p_index] = data[count];
91+
data[p_index] = std::move(data[count]);
9292
}
9393
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
9494
data[count].~T();
@@ -245,13 +245,13 @@ class LocalVector {
245245
void insert(U p_pos, T p_val) {
246246
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
247247
if (p_pos == count) {
248-
push_back(p_val);
248+
push_back(std::move(p_val));
249249
} else {
250250
resize(count + 1);
251251
for (U i = count - 1; i > p_pos; i--) {
252-
data[i] = data[i - 1];
252+
data[i] = std::move(data[i - 1]);
253253
}
254-
data[p_pos] = p_val;
254+
data[p_pos] = std::move(p_val);
255255
}
256256
}
257257

0 commit comments

Comments
 (0)