Skip to content

Commit f50d7fa

Browse files
committed
Merge pull request godotengine#110709 from DeeJayLSP/dict-reserve
Add `reserve()` to `Dictionary`, apply to constructors on GDScript VM
2 parents 464ddd4 + 8a7a0fa commit f50d7fa

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
@@ -305,6 +305,12 @@ void Dictionary::_ref(const Dictionary &p_from) const {
305305
_p = p_from._p;
306306
}
307307

308+
void Dictionary::reserve(int p_new_capacity) {
309+
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
310+
ERR_FAIL_COND_MSG(p_new_capacity < 0, "New capacity must be non-negative.");
311+
_p->variant_map.reserve(p_new_capacity);
312+
}
313+
308314
void Dictionary::clear() {
309315
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
310316
_p->variant_map.clear();
@@ -613,6 +619,7 @@ Dictionary Dictionary::recursive_duplicate(bool p_deep, ResourceDeepDuplicateMod
613619
return n;
614620
}
615621

622+
n.reserve(_p->variant_map.size());
616623
if (p_deep) {
617624
bool is_call_chain_end = recursion_count == 0;
618625

core/variant/dictionary.h

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

7373
int size() const;
7474
bool is_empty() const;
75+
void reserve(int p_new_capacity);
7576
void clear();
7677
void sort();
7778
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)