Skip to content

Commit 8a7a0fa

Browse files
committed
Add reserve() to Dictionary, apply to constructors on GDScript VM
1 parent aa2c4fe commit 8a7a0fa

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

core/variant/dictionary.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ void Dictionary::_ref(const Dictionary &p_from) const {
299299
_p = p_from._p;
300300
}
301301

302+
void Dictionary::reserve(int p_new_capacity) {
303+
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
304+
ERR_FAIL_COND_MSG(p_new_capacity < 0, "New capacity must be non-negative.");
305+
_p->variant_map.reserve(p_new_capacity);
306+
}
307+
302308
void Dictionary::clear() {
303309
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
304310
_p->variant_map.clear();
@@ -607,6 +613,7 @@ Dictionary Dictionary::recursive_duplicate(bool p_deep, ResourceDeepDuplicateMod
607613
return n;
608614
}
609615

616+
n.reserve(_p->variant_map.size());
610617
if (p_deep) {
611618
bool is_call_chain_end = recursion_count == 0;
612619

core/variant/dictionary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Dictionary {
7474

7575
int size() const;
7676
bool is_empty() const;
77+
void reserve(int p_new_capacity);
7778
void clear();
7879
void sort();
7980
void merge(const Dictionary &p_dictionary, bool p_overwrite = false);

modules/gdscript/gdscript_vm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
18291829

18301830
int argc = _code_ptr[ip + 1];
18311831
Dictionary dict;
1832-
1832+
dict.reserve(argc);
18331833
for (int i = 0; i < argc; i++) {
18341834
GET_INSTRUCTION_ARG(k, i * 2 + 0);
18351835
GET_INSTRUCTION_ARG(v, i * 2 + 1);
@@ -1867,7 +1867,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
18671867

18681868
Dictionary dict;
18691869
dict.set_typed(key_builtin_type, key_native_type, *key_script_type, value_builtin_type, value_native_type, *value_script_type);
1870-
1870+
dict.reserve(argc);
18711871
for (int i = 0; i < argc; i++) {
18721872
GET_INSTRUCTION_ARG(k, i * 2 + 0);
18731873
GET_INSTRUCTION_ARG(v, i * 2 + 1);

0 commit comments

Comments
 (0)