Don't set Variant::Type in destructor

When profiling Dome Keeper, I found that in physics_process a HashMap
gets cleared a lot, which ends up calling the Variant destructor.
Calling Variant::clear() dominates this operation.

By not uselessly setting the Type to NIL on destruction we save about
50% of time. This is likely because if there is a simple type in the
Variant that doesn't need destructing, but now we write when we should
just drop the Variant altogether.

Since the value of Variant::type should be unobservable after
destruction this doesn't change any outward behavior.
This commit is contained in:
HP van Braam 2024-12-25 01:03:15 +01:00
parent 0f95e9f8e6
commit 46c23e1758

View file

@ -275,8 +275,7 @@ private:
void _clear_internal();
_FORCE_INLINE_ void clear() {
static const bool needs_deinit[Variant::VARIANT_MAX] = {
static constexpr bool needs_deinit[Variant::VARIANT_MAX] = {
false, //NIL,
false, //BOOL,
false, //INT,
@ -322,6 +321,7 @@ private:
true, //PACKED_VECTOR4_ARRAY,
};
_FORCE_INLINE_ void clear() {
if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
_clear_internal();
}
@ -832,7 +832,9 @@ public:
}
_FORCE_INLINE_ Variant() {}
_FORCE_INLINE_ ~Variant() {
clear();
if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
_clear_internal();
}
}
};