Skip to content

Commit 18a17de

Browse files
committed
Merge pull request #112630 from aaronp64/push_back_move
Avoid extra copy in `Vector`/`CowData` `push_back`/`insert`
2 parents 72925d7 + afd59b3 commit 18a17de

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

core/templates/cowdata.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ class CowData {
206206

207207
_FORCE_INLINE_ void remove_at(Size p_index);
208208

209-
Error insert(Size p_pos, const T &p_val);
210-
Error push_back(const T &p_val);
209+
Error insert(Size p_pos, T &&p_val);
210+
Error push_back(T &&p_val);
211211

212212
_FORCE_INLINE_ operator Span<T>() const { return Span<T>(ptr(), size()); }
213213
_FORCE_INLINE_ Span<T> span() const { return operator Span<T>(); }
@@ -296,7 +296,7 @@ void CowData<T>::remove_at(Size p_index) {
296296
}
297297

298298
template <typename T>
299-
Error CowData<T>::insert(Size p_pos, const T &p_val) {
299+
Error CowData<T>::insert(Size p_pos, T &&p_val) {
300300
const Size new_size = size() + 1;
301301
ERR_FAIL_INDEX_V(p_pos, new_size, ERR_INVALID_PARAMETER);
302302

@@ -326,13 +326,13 @@ Error CowData<T>::insert(Size p_pos, const T &p_val) {
326326
}
327327

328328
// Create the new element at the given index.
329-
memnew_placement(_ptr + p_pos, T(p_val));
329+
memnew_placement(_ptr + p_pos, T(std::move(p_val)));
330330

331331
return OK;
332332
}
333333

334334
template <typename T>
335-
Error CowData<T>::push_back(const T &p_val) {
335+
Error CowData<T>::push_back(T &&p_val) {
336336
const Size new_size = size() + 1;
337337

338338
if (!_ptr) {
@@ -361,7 +361,7 @@ Error CowData<T>::push_back(const T &p_val) {
361361
}
362362

363363
// Create the new element at the given index.
364-
memnew_placement(_ptr + new_size - 1, T(p_val));
364+
memnew_placement(_ptr + new_size - 1, T(std::move(p_val)));
365365

366366
return OK;
367367
}

core/templates/vector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class Vector {
7171

7272
public:
7373
// Must take a copy instead of a reference (see GH-31736).
74-
_FORCE_INLINE_ bool push_back(T p_elem) { return _cowdata.push_back(p_elem); }
75-
_FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
74+
_FORCE_INLINE_ bool push_back(T p_elem) { return _cowdata.push_back(std::move(p_elem)); }
75+
_FORCE_INLINE_ bool append(T p_elem) { return _cowdata.push_back(std::move(p_elem)); } //alias
7676
void fill(T p_elem);
7777

7878
void remove_at(Size p_index) { _cowdata.remove_at(p_index); }
@@ -133,7 +133,7 @@ class Vector {
133133

134134
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
135135
// Must take a copy instead of a reference (see GH-31736).
136-
Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
136+
Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, std::move(p_val)); }
137137
Size find(const T &p_val, Size p_from = 0) const {
138138
if (p_from < 0) {
139139
p_from = size() + p_from;

0 commit comments

Comments
 (0)