Skip to content

Commit aa2c4fe

Browse files
committed
Merge pull request godotengine#110717 from Shadows-of-Fire/elide-copy-typed-collection
GDScript: Elide unnecessary copies in `CONSTRUCT_TYPED_*` opcodes
2 parents 6010f0f + b18beb2 commit aa2c4fe

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

modules/gdscript/gdscript_vm.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,15 +1805,17 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
18051805
const StringName native_type = _global_names_ptr[native_type_idx];
18061806

18071807
Array array;
1808+
array.set_typed(builtin_type, native_type, *script_type);
18081809
array.resize(argc);
18091810
for (int i = 0; i < argc; i++) {
1810-
array[i] = *(instruction_args[i]);
1811+
// Use .set instead of operator[] to handle type conversion / validation.
1812+
array.set(i, *(instruction_args[i]));
18111813
}
18121814

18131815
GET_INSTRUCTION_ARG(dst, argc);
18141816
*dst = Variant(); // Clear potential previous typed array.
18151817

1816-
*dst = Array(array, builtin_type, native_type, *script_type);
1818+
*dst = array;
18171819

18181820
ip += 4;
18191821
}
@@ -1864,18 +1866,20 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
18641866
const StringName value_native_type = _global_names_ptr[value_native_type_idx];
18651867

18661868
Dictionary dict;
1869+
dict.set_typed(key_builtin_type, key_native_type, *key_script_type, value_builtin_type, value_native_type, *value_script_type);
18671870

18681871
for (int i = 0; i < argc; i++) {
18691872
GET_INSTRUCTION_ARG(k, i * 2 + 0);
18701873
GET_INSTRUCTION_ARG(v, i * 2 + 1);
1871-
dict[*k] = *v;
1874+
// Use .set instead of operator[] to handle type conversion / validation.
1875+
dict.set(*k, *v);
18721876
}
18731877

18741878
GET_INSTRUCTION_ARG(dst, argc * 2);
18751879

18761880
*dst = Variant(); // Clear potential previous typed dictionary.
18771881

1878-
*dst = Dictionary(dict, key_builtin_type, key_native_type, *key_script_type, value_builtin_type, value_native_type, *value_script_type);
1882+
*dst = dict;
18791883

18801884
ip += 6;
18811885
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
GDTEST_RUNTIME_ERROR
22
>> ERROR: Method/function failed. Returning: false
3-
>> Attempted to assign an object into a TypedArray, that does not inherit from 'GDScript'.
4-
>> ERROR: Method/function failed.
5-
>> Unable to convert array index 0 from "Object" to "Object".
3+
>> Attempted to set an object into a TypedArray, that does not inherit from 'GDScript'.
4+
>> ERROR: Condition "!_p->typed.validate(value, "set")" is true.
65
not ok
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
GDTEST_RUNTIME_ERROR
22
>> ERROR: Method/function failed. Returning: false
3-
>> Attempted to assign an object into a TypedDictionary.Key, that does not inherit from 'GDScript'.
4-
>> ERROR: Method/function failed.
5-
>> Unable to convert key from "Object" to "Object".
3+
>> Attempted to set an object into a TypedDictionary.Key, that does not inherit from 'GDScript'.
4+
>> ERROR: Condition "!_p->typed_key.validate(key, "set")" is true. Returning: false
65
not ok

0 commit comments

Comments
 (0)