From 8a7a0faa75b573946aac9971c93fca3b6c7b3193 Mon Sep 17 00:00:00 2001 From: DeeJayLSP Date: Tue, 23 Sep 2025 16:57:52 -0300 Subject: [PATCH] Add `reserve()` to `Dictionary`, apply to constructors on GDScript VM --- core/variant/dictionary.cpp | 7 +++++++ core/variant/dictionary.h | 1 + modules/gdscript/gdscript_vm.cpp | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index ea110477f2a..8030029dd94 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -299,6 +299,12 @@ void Dictionary::_ref(const Dictionary &p_from) const { _p = p_from._p; } +void Dictionary::reserve(int p_new_capacity) { + ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state."); + ERR_FAIL_COND_MSG(p_new_capacity < 0, "New capacity must be non-negative."); + _p->variant_map.reserve(p_new_capacity); +} + void Dictionary::clear() { ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state."); _p->variant_map.clear(); @@ -607,6 +613,7 @@ Dictionary Dictionary::recursive_duplicate(bool p_deep, ResourceDeepDuplicateMod return n; } + n.reserve(_p->variant_map.size()); if (p_deep) { bool is_call_chain_end = recursion_count == 0; diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index 5558c4e2089..e35097cb1c3 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -74,6 +74,7 @@ public: int size() const; bool is_empty() const; + void reserve(int p_new_capacity); void clear(); void sort(); void merge(const Dictionary &p_dictionary, bool p_overwrite = false); diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cpp index 2204b4021a6..f97ed382b6b 100644 --- a/modules/gdscript/gdscript_vm.cpp +++ b/modules/gdscript/gdscript_vm.cpp @@ -1829,7 +1829,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a int argc = _code_ptr[ip + 1]; Dictionary dict; - + dict.reserve(argc); for (int i = 0; i < argc; i++) { GET_INSTRUCTION_ARG(k, i * 2 + 0); GET_INSTRUCTION_ARG(v, i * 2 + 1); @@ -1867,7 +1867,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Dictionary dict; dict.set_typed(key_builtin_type, key_native_type, *key_script_type, value_builtin_type, value_native_type, *value_script_type); - + dict.reserve(argc); for (int i = 0; i < argc; i++) { GET_INSTRUCTION_ARG(k, i * 2 + 0); GET_INSTRUCTION_ARG(v, i * 2 + 1);