Skip to content

Commit 3e17771

Browse files
committed
Expose reserve() to Dictionary, use it on the GDScript VM
1 parent 3c9d03b commit 3e17771

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

core/variant/dictionary.cpp

Lines changed: 6 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(int64_t 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 || p_new_capacity > UINT32_MAX, "New capacity must be non-negative and less than or equal to UINT32_MAX.");
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();

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(int64_t 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
@@ -1827,7 +1827,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
18271827

18281828
int argc = _code_ptr[ip + 1];
18291829
Dictionary dict;
1830-
1830+
dict.reserve(argc);
18311831
for (int i = 0; i < argc; i++) {
18321832
GET_INSTRUCTION_ARG(k, i * 2 + 0);
18331833
GET_INSTRUCTION_ARG(v, i * 2 + 1);
@@ -1864,7 +1864,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
18641864
const StringName value_native_type = _global_names_ptr[value_native_type_idx];
18651865

18661866
Dictionary dict;
1867-
1867+
dict.reserve(argc);
18681868
for (int i = 0; i < argc; i++) {
18691869
GET_INSTRUCTION_ARG(k, i * 2 + 0);
18701870
GET_INSTRUCTION_ARG(v, i * 2 + 1);

0 commit comments

Comments
 (0)