summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authoraaronp64 <aaronp.code@gmail.com>2024-09-16 13:07:13 -0400
committeraaronp64 <aaronp.code@gmail.com>2024-09-16 15:47:58 -0400
commite2b6d92e27fbe56331ccf10b2d70e793521d5058 (patch)
tree43544d103d0bb004e97d06b981fc6a832221c0b4 /modules
parent6681f2563b99e14929a8acb27f4908fece398ef1 (diff)
downloadredot-engine-e2b6d92e27fbe56331ccf10b2d70e793521d5058.tar.gz
Speed up lambda capture handling
Updated GDScriptLambdaCallable::call and GDScriptLambdaSelfCallable::call to use alloca instead of Vector when using captures, to avoid extra allocation/copy_on_write calls on each lambda function call.
Diffstat (limited to 'modules')
-rw-r--r--modules/gdscript/gdscript_lambda_callable.cpp24
1 files changed, 12 insertions, 12 deletions
diff --git a/modules/gdscript/gdscript_lambda_callable.cpp b/modules/gdscript/gdscript_lambda_callable.cpp
index 2de5811bca..d6fd5d043b 100644
--- a/modules/gdscript/gdscript_lambda_callable.cpp
+++ b/modules/gdscript/gdscript_lambda_callable.cpp
@@ -97,25 +97,25 @@ void GDScriptLambdaCallable::call(const Variant **p_arguments, int p_argcount, V
}
if (captures_amount > 0) {
- Vector<const Variant *> args;
- args.resize(p_argcount + captures_amount);
+ const int total_argcount = p_argcount + captures_amount;
+ const Variant **args = (const Variant **)alloca(sizeof(Variant *) * total_argcount);
for (int i = 0; i < captures_amount; i++) {
- args.write[i] = &captures[i];
+ args[i] = &captures[i];
if (captures[i].get_type() == Variant::OBJECT) {
bool was_freed = false;
captures[i].get_validated_object_with_check(was_freed);
if (was_freed) {
ERR_PRINT(vformat(R"(Lambda capture at index %d was freed. Passed "null" instead.)", i));
static Variant nil;
- args.write[i] = &nil;
+ args[i] = &nil;
}
}
}
for (int i = 0; i < p_argcount; i++) {
- args.write[i + captures_amount] = p_arguments[i];
+ args[i + captures_amount] = p_arguments[i];
}
- r_return_value = function->call(nullptr, args.ptrw(), args.size(), r_call_error);
+ r_return_value = function->call(nullptr, args, total_argcount, r_call_error);
switch (r_call_error.error) {
case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
r_call_error.argument -= captures_amount;
@@ -229,25 +229,25 @@ void GDScriptLambdaSelfCallable::call(const Variant **p_arguments, int p_argcoun
}
if (captures_amount > 0) {
- Vector<const Variant *> args;
- args.resize(p_argcount + captures_amount);
+ const int total_argcount = p_argcount + captures_amount;
+ const Variant **args = (const Variant **)alloca(sizeof(Variant *) * total_argcount);
for (int i = 0; i < captures_amount; i++) {
- args.write[i] = &captures[i];
+ args[i] = &captures[i];
if (captures[i].get_type() == Variant::OBJECT) {
bool was_freed = false;
captures[i].get_validated_object_with_check(was_freed);
if (was_freed) {
ERR_PRINT(vformat(R"(Lambda capture at index %d was freed. Passed "null" instead.)", i));
static Variant nil;
- args.write[i] = &nil;
+ args[i] = &nil;
}
}
}
for (int i = 0; i < p_argcount; i++) {
- args.write[i + captures_amount] = p_arguments[i];
+ args[i + captures_amount] = p_arguments[i];
}
- r_return_value = function->call(static_cast<GDScriptInstance *>(object->get_script_instance()), args.ptrw(), args.size(), r_call_error);
+ r_return_value = function->call(static_cast<GDScriptInstance *>(object->get_script_instance()), args, total_argcount, r_call_error);
switch (r_call_error.error) {
case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
r_call_error.argument -= captures_amount;