From 6609caf76e8739b59bc7e073b4ebc801d647b0b9 Mon Sep 17 00:00:00 2001 From: Nazarii Date: Mon, 30 Dec 2024 14:55:29 +0200 Subject: [PATCH] Use 1.5x growth factor for LocalVector --- core/templates/local_vector.h | 22 ++++++++++----------- servers/rendering/rendering_device_driver.h | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/templates/local_vector.h b/core/templates/local_vector.h index d20fe49eba6..6f1ac927ec3 100644 --- a/core/templates/local_vector.h +++ b/core/templates/local_vector.h @@ -59,9 +59,7 @@ public: _FORCE_INLINE_ void push_back(T p_elem) { if (unlikely(count == capacity)) { - capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1); - data = (T *)memrealloc(data, capacity * sizeof(T)); - CRASH_COND_MSG(!data, "Out of memory"); + reserve(count + 1); } if constexpr (!std::is_trivially_constructible_v && !force_trivial) { @@ -137,10 +135,16 @@ public: } _FORCE_INLINE_ bool is_empty() const { return count == 0; } _FORCE_INLINE_ U get_capacity() const { return capacity; } - _FORCE_INLINE_ void reserve(U p_size) { - p_size = tight ? p_size : nearest_power_of_2_templated(p_size); + void reserve(U p_size) { if (p_size > capacity) { - capacity = p_size; + if (tight) { + capacity = p_size; + } else { + capacity = MAX((U)2, capacity + ((1 + capacity) >> 1)); + if (p_size > capacity) { + capacity = p_size; + } + } data = (T *)memrealloc(data, capacity * sizeof(T)); CRASH_COND_MSG(!data, "Out of memory"); } @@ -156,11 +160,7 @@ public: } count = p_size; } else if (p_size > count) { - if (unlikely(p_size > capacity)) { - capacity = tight ? p_size : nearest_power_of_2_templated(p_size); - data = (T *)memrealloc(data, capacity * sizeof(T)); - CRASH_COND_MSG(!data, "Out of memory"); - } + reserve(p_size); if constexpr (!std::is_trivially_constructible_v && !force_trivial) { for (U i = count; i < p_size; i++) { memnew_placement(&data[i], T); diff --git a/servers/rendering/rendering_device_driver.h b/servers/rendering/rendering_device_driver.h index 2f6ea79316b..7bd77171cb2 100644 --- a/servers/rendering/rendering_device_driver.h +++ b/servers/rendering/rendering_device_driver.h @@ -614,7 +614,7 @@ public: }; struct AttachmentReference { - static const uint32_t UNUSED = 0xffffffff; + static constexpr uint32_t UNUSED = 0xffffffff; uint32_t attachment = UNUSED; TextureLayout layout = TEXTURE_LAYOUT_UNDEFINED; BitField aspect;