mirror of https://github.com/godotengine/godot
Optimize `CowData` and `LocalVector` functions `.insert` and `.remove_at` by using move semantics.
This commit is contained in:
parent
b9437c3938
commit
a636c04244
|
|
@ -37,6 +37,7 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Vector;
|
class Vector;
|
||||||
|
|
@ -224,7 +225,7 @@ public:
|
||||||
T *p = ptrw();
|
T *p = ptrw();
|
||||||
Size len = size();
|
Size len = size();
|
||||||
for (Size i = p_index; i < len - 1; i++) {
|
for (Size i = p_index; i < len - 1; i++) {
|
||||||
p[i] = p[i + 1];
|
p[i] = std::move(p[i + 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
resize(len - 1);
|
resize(len - 1);
|
||||||
|
|
@ -237,7 +238,7 @@ public:
|
||||||
ERR_FAIL_COND_V(err, err);
|
ERR_FAIL_COND_V(err, err);
|
||||||
T *p = ptrw();
|
T *p = ptrw();
|
||||||
for (Size i = new_size - 1; i > p_pos; i--) {
|
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;
|
p[p_pos] = p_val;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ public:
|
||||||
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
|
if constexpr (!std::is_trivially_constructible_v<T> && !force_trivial) {
|
||||||
memnew_placement(&data[count++], T(p_elem));
|
memnew_placement(&data[count++], T(p_elem));
|
||||||
} else {
|
} else {
|
||||||
data[count++] = p_elem;
|
data[count++] = std::move(p_elem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,7 +75,7 @@ public:
|
||||||
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
|
ERR_FAIL_UNSIGNED_INDEX(p_index, count);
|
||||||
count--;
|
count--;
|
||||||
for (U i = p_index; i < count; i++) {
|
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) {
|
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
|
||||||
data[count].~T();
|
data[count].~T();
|
||||||
|
|
@ -88,7 +88,7 @@ public:
|
||||||
ERR_FAIL_INDEX(p_index, count);
|
ERR_FAIL_INDEX(p_index, count);
|
||||||
count--;
|
count--;
|
||||||
if (count > p_index) {
|
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) {
|
if constexpr (!std::is_trivially_destructible_v<T> && !force_trivial) {
|
||||||
data[count].~T();
|
data[count].~T();
|
||||||
|
|
@ -245,13 +245,13 @@ public:
|
||||||
void insert(U p_pos, T p_val) {
|
void insert(U p_pos, T p_val) {
|
||||||
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
|
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
|
||||||
if (p_pos == count) {
|
if (p_pos == count) {
|
||||||
push_back(p_val);
|
push_back(std::move(p_val));
|
||||||
} else {
|
} else {
|
||||||
resize(count + 1);
|
resize(count + 1);
|
||||||
for (U i = count - 1; i > p_pos; i--) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue