Skip to content

Commit 8bcec7a

Browse files
committed
Merge pull request godotengine#106730 from Ivorforce/simplify-memnew-arr-placement
Simplify `Memory::memnew_arr_placement` to always initialize memory
2 parents 1c151e9 + 4371aa8 commit 8bcec7a

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

core/os/memory.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,15 @@ T *memnew_arr_template(size_t p_elements) {
196196
}
197197

198198
// Fast alternative to a loop constructor pattern.
199-
template <bool p_ensure_zero = false, typename T>
199+
template <typename T>
200200
_FORCE_INLINE_ void memnew_arr_placement(T *p_start, size_t p_num) {
201-
if constexpr (std::is_trivially_constructible_v<T> && !p_ensure_zero) {
202-
// Don't need to do anything :)
203-
(void)p_start;
204-
(void)p_num;
205-
} else if constexpr (is_zero_constructible_v<T>) {
201+
if constexpr (is_zero_constructible_v<T>) {
206202
// Can optimize with memset.
207203
memset(static_cast<void *>(p_start), 0, p_num * sizeof(T));
208204
} else {
209205
// Need to use a for loop.
210206
for (size_t i = 0; i < p_num; i++) {
211-
memnew_placement(p_start + i, T);
207+
memnew_placement(p_start + i, T());
212208
}
213209
}
214210
}

core/string/ustring.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class [[nodiscard]] CharStringT {
182182
_FORCE_INLINE_ operator Span<T>() const { return Span(ptr(), length()); }
183183
_FORCE_INLINE_ Span<T> span() const { return Span(ptr(), length()); }
184184

185-
_FORCE_INLINE_ Error resize(int p_size) { return _cowdata.resize(p_size); }
185+
_FORCE_INLINE_ Error resize(int p_size) { return _cowdata.template resize<false>(p_size); }
186186

187187
_FORCE_INLINE_ T get(int p_index) const { return _cowdata.get(p_index); }
188188
_FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
@@ -324,7 +324,7 @@ class [[nodiscard]] String {
324324

325325
_FORCE_INLINE_ char32_t get(int p_index) const { return _cowdata.get(p_index); }
326326
_FORCE_INLINE_ void set(int p_index, const char32_t &p_elem) { _cowdata.set(p_index, p_elem); }
327-
Error resize(int p_size) { return _cowdata.resize(p_size); }
327+
Error resize(int p_size) { return _cowdata.resize<false>(p_size); }
328328

329329
_FORCE_INLINE_ const char32_t &operator[](int p_index) const {
330330
if (unlikely(p_index == _cowdata.size())) {

core/templates/cowdata.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class CowData {
208208
return _ptr[p_index];
209209
}
210210

211-
template <bool p_ensure_zero = false>
211+
template <bool p_initialize = true>
212212
Error resize(Size p_size);
213213

214214
_FORCE_INLINE_ void remove_at(Size p_index) {
@@ -366,7 +366,7 @@ Error CowData<T>::_fork_allocate(USize p_size) {
366366
}
367367

368368
template <typename T>
369-
template <bool p_ensure_zero>
369+
template <bool p_initialize>
370370
Error CowData<T>::resize(Size p_size) {
371371
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
372372

@@ -380,8 +380,12 @@ Error CowData<T>::resize(Size p_size) {
380380
return error;
381381
}
382382

383-
if (p_size > prev_size) {
384-
memnew_arr_placement<p_ensure_zero>(_ptr + prev_size, p_size - prev_size);
383+
if constexpr (p_initialize) {
384+
if (p_size > prev_size) {
385+
memnew_arr_placement(_ptr + prev_size, p_size - prev_size);
386+
}
387+
} else {
388+
static_assert(std::is_trivially_destructible_v<T>);
385389
}
386390

387391
return OK;

core/templates/fixed_vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class FixedVector {
107107
constexpr Error resize_initialized(uint32_t p_size) {
108108
if (p_size > _size) {
109109
ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
110-
memnew_arr_placement<true>(ptr() + _size, p_size - _size);
110+
memnew_arr_placement(ptr() + _size, p_size - _size);
111111
} else if (p_size < _size) {
112112
if constexpr (!std::is_trivially_destructible_v<T>) {
113113
for (uint32_t i = p_size; i < _size; i++) {

core/templates/vector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ class Vector {
9393
_FORCE_INLINE_ operator Span<T>() const { return _cowdata.span(); }
9494
_FORCE_INLINE_ Span<T> span() const { return _cowdata.span(); }
9595

96-
_FORCE_INLINE_ void clear() { resize(0); }
96+
_FORCE_INLINE_ void clear() { _cowdata.clear(); }
9797
_FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
9898

9999
_FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); }
100100
_FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); }
101101
_FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
102-
Error resize(Size p_size) { return _cowdata.resize(p_size); }
102+
Error resize(Size p_size) { return _cowdata.template resize<!std::is_trivially_constructible_v<T>>(p_size); }
103103
Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); }
104104
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
105105
// Must take a copy instead of a reference (see GH-31736).

0 commit comments

Comments
 (0)