Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions godot-core/src/builtin/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,14 @@ mod custom_callable {
) {
let arg_refs: &[&Variant] = Variant::borrow_ref_slice(p_args, p_argument_count as usize);

let name = {
#[cfg(debug_assertions)]
let name = &{
let c: &C = CallableUserdata::inner_from_raw(callable_userdata);
c.to_string()
};
let ctx = meta::CallContext::custom_callable(name.as_str());
#[cfg(not(debug_assertions))]
let name = "<optimized out>";
let ctx = meta::CallContext::custom_callable(name);

crate::private::handle_varcall_panic(&ctx, &mut *r_error, move || {
// Get the RustCallable again inside closure so it doesn't have to be UnwindSafe.
Expand All @@ -681,11 +684,14 @@ mod custom_callable {
{
let arg_refs: &[&Variant] = Variant::borrow_ref_slice(p_args, p_argument_count as usize);

let name = {
#[cfg(debug_assertions)]
let name = &{
let w: &FnWrapper<F> = CallableUserdata::inner_from_raw(callable_userdata);
w.name.to_string()
};
let ctx = meta::CallContext::custom_callable(name.as_str());
#[cfg(not(debug_assertions))]
let name = "<optimized out>";
let ctx = meta::CallContext::custom_callable(name);

crate::private::handle_varcall_panic(&ctx, &mut *r_error, move || {
// Get the FnWrapper again inside closure so the FnMut doesn't have to be UnwindSafe.
Expand Down
28 changes: 28 additions & 0 deletions itest/rust/src/benchmarks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use godot::builtin::inner::InnerRect2i;
use godot::builtin::{GString, PackedInt32Array, Rect2i, StringName, Vector2i};
use godot::classes::{Node3D, Os, RefCounted};
use godot::obj::{Gd, InstanceId, NewAlloc, NewGd, Singleton};
use godot::prelude::{varray, Callable, RustCallable, Variant};
use godot::register::GodotClass;

use crate::framework::bench;
Expand Down Expand Up @@ -113,9 +114,36 @@ fn packed_array_from_iter_unknown_size() -> PackedInt32Array {
}))
}

#[bench(repeat = 25)]
fn call_callv_rust_fn() -> Variant {
let callable = Callable::from_fn("RustFunction", |_| ());
callable.callv(&varray![])
}

#[bench(repeat = 25)]
fn call_callv_custom() -> Variant {
let callable = Callable::from_custom(MyRustCallable {});
callable.callv(&varray![])
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Helpers for benchmarks above

#[derive(GodotClass)]
#[class(init)]
struct MyBenchType {}

#[derive(PartialEq, Hash)]
struct MyRustCallable {}

impl RustCallable for MyRustCallable {
fn invoke(&mut self, _args: &[&Variant]) -> Variant {
Variant::nil()
}
}

impl std::fmt::Display for MyRustCallable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MyRustCallable")
}
}
Loading