Compare commits

...

2 Commits

Author SHA1 Message Date
David Snopek 6e624af301
Merge 9b3bcee71d into 36847f6af0 2024-01-31 21:38:44 +00:00
David Snopek 9b3bcee71d Avoid creating objects that Godot is going to use placement new to initialize 2024-01-31 15:38:32 -06:00
2 changed files with 25 additions and 6 deletions

View File

@ -190,6 +190,25 @@ struct _GlobalNilClass {
static _GlobalNil _nil; static _GlobalNil _nil;
}; };
template <class T, size_t Alignment = alignof(T)>
class UninitializedValue {
typename std::aligned_storage<sizeof(T), Alignment>::type data;
public:
UninitializedValue() {}
UninitializedValue(const UninitializedValue &) = delete;
UninitializedValue &operator=(const UninitializedValue &) = delete;
T &get() {
return *reinterpret_cast<T *>(&data);
}
T *ptr() {
return reinterpret_cast<T *>(&data);
}
};
} // namespace godot } // namespace godot
#endif // GODOT_MEMORY_HPP #endif // GODOT_MEMORY_HPP

View File

@ -459,15 +459,15 @@ Variant::operator Signal() const {
} }
Variant::operator Dictionary() const { Variant::operator Dictionary() const {
Dictionary result; UninitializedValue<Dictionary> result;
to_type_constructor[DICTIONARY](result._native_ptr(), _native_ptr()); to_type_constructor[DICTIONARY](result.ptr(), _native_ptr());
return result; return result.get();
} }
Variant::operator Array() const { Variant::operator Array() const {
Array result; UninitializedValue<Array> result;
to_type_constructor[ARRAY](result._native_ptr(), _native_ptr()); to_type_constructor[ARRAY](result.ptr(), _native_ptr());
return result; return result.get();
} }
Variant::operator PackedByteArray() const { Variant::operator PackedByteArray() const {