Skip to content

Commit a702b19

Browse files
committed
Only provide function name to CallContext in debug
1 parent fd043d5 commit a702b19

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

godot-core/src/builtin/callable.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ impl Callable {
162162
F: 'static + FnMut(&[&Variant]) -> R,
163163
S: meta::AsArg<GString>,
164164
{
165+
#[cfg(debug_assertions)]
165166
meta::arg_into_owned!(name);
166167

167168
Self::from_fn_wrapper::<F, R>(FnWrapper {
168169
rust_function,
170+
#[cfg(debug_assertions)]
169171
name,
170172
thread_id: Some(std::thread::current().id()),
171173
linked_obj_id: None,
@@ -187,10 +189,12 @@ impl Callable {
187189
F: 'static + FnMut(&[&Variant]) -> R,
188190
S: meta::AsArg<GString>,
189191
{
192+
#[cfg(debug_assertions)]
190193
meta::arg_into_owned!(name);
191194

192195
Self::from_fn_wrapper::<F, R>(FnWrapper {
193196
rust_function,
197+
#[cfg(debug_assertions)]
194198
name,
195199
thread_id: Some(std::thread::current().id()),
196200
linked_obj_id: Some(linked_object.instance_id()),
@@ -254,10 +258,12 @@ impl Callable {
254258
F: FnMut(&[&Variant]) -> Variant,
255259
Fc: FnOnce(&Callable) -> R,
256260
{
261+
#[cfg(debug_assertions)]
257262
meta::arg_into_owned!(name);
258263

259264
let callable = Self::from_fn_wrapper::<F, Variant>(FnWrapper {
260265
rust_function,
266+
#[cfg(debug_assertions)]
261267
name,
262268
thread_id: Some(std::thread::current().id()),
263269
linked_obj_id: None,
@@ -292,10 +298,12 @@ impl Callable {
292298
F: 'static + Send + Sync + FnMut(&[&Variant]) -> R,
293299
S: meta::AsArg<GString>,
294300
{
301+
#[cfg(debug_assertions)]
295302
meta::arg_into_owned!(name);
296303

297304
Self::from_fn_wrapper::<F, R>(FnWrapper {
298305
rust_function,
306+
#[cfg(debug_assertions)]
299307
name,
300308
thread_id: None,
301309
linked_obj_id: None,
@@ -340,6 +348,7 @@ impl Callable {
340348
callable_userdata: Box::into_raw(Box::new(userdata)) as *mut std::ffi::c_void,
341349
call_func: Some(rust_callable_call_fn::<F, R>),
342350
free_func: Some(rust_callable_destroy::<FnWrapper<F>>),
351+
#[cfg(debug_assertions)]
343352
to_string_func: Some(rust_callable_to_string_named::<F>),
344353
is_valid_func: Some(rust_callable_is_valid),
345354
..Self::default_callable_custom_info()
@@ -446,13 +455,9 @@ impl Callable {
446455
InstanceId::try_from_i64(id)
447456
}
448457

449-
crate::declare_hash_u32_method! {
450-
/// Returns the 32-bit hash value of this callable's object.
451-
///
452-
/// _Godot equivalent: `hash`_
453-
}
454-
455-
#[deprecated = "renamed to hash_u32"]
458+
/// Returns the 32-bit hash value of this callable's object.
459+
///
460+
/// _Godot equivalent: `hash`_
456461
pub fn hash(&self) -> u32 {
457462
self.as_inner().hash().try_into().unwrap()
458463
}
@@ -608,6 +613,7 @@ mod custom_callable {
608613

609614
pub(crate) struct FnWrapper<F> {
610615
pub(super) rust_function: F,
616+
#[cfg(debug_assertions)]
611617
pub(super) name: GString,
612618

613619
/// `None` if the callable is multi-threaded ([`Callable::from_sync_fn`]).
@@ -658,11 +664,14 @@ mod custom_callable {
658664
) {
659665
let arg_refs: &[&Variant] = Variant::borrow_ref_slice(p_args, p_argument_count as usize);
660666

661-
let name = {
667+
#[cfg(debug_assertions)]
668+
let name = &{
662669
let c: &C = CallableUserdata::inner_from_raw(callable_userdata);
663670
c.to_string()
664671
};
665-
let ctx = meta::CallContext::custom_callable(name.as_str());
672+
#[cfg(not(debug_assertions))]
673+
let name = "<optimized out>";
674+
let ctx = meta::CallContext::custom_callable(name);
666675

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

688-
let name = {
697+
#[cfg(debug_assertions)]
698+
let name = &{
689699
let w: &FnWrapper<F> = CallableUserdata::inner_from_raw(callable_userdata);
690700
w.name.to_string()
691701
};
692-
let ctx = meta::CallContext::custom_callable(name.as_str());
702+
#[cfg(not(debug_assertions))]
703+
let name = "<optimized out>";
704+
let ctx = meta::CallContext::custom_callable(name);
693705

694706
crate::private::handle_varcall_panic(&ctx, &mut *r_error, move || {
695707
// Get the FnWrapper again inside closure so the FnMut doesn't have to be UnwindSafe.
@@ -698,12 +710,16 @@ mod custom_callable {
698710
if w.thread_id
699711
.is_some_and(|tid| tid != std::thread::current().id())
700712
{
713+
#[cfg(debug_assertions)]
714+
let name = &w.name;
715+
#[cfg(not(debug_assertions))]
716+
let name = "<optimized out>";
701717
// NOTE: this panic is currently not propagated to the caller, but results in an error message and Nil return.
702718
// See comments in itest callable_call() for details.
703719
panic!(
704720
"Callable '{}' created with from_fn() must be called from the same thread it was created in.\n\
705721
If you need to call it from any thread, use from_sync_fn() instead (requires `experimental-threads` feature).",
706-
w.name
722+
name
707723
);
708724
}
709725

@@ -749,6 +765,7 @@ mod custom_callable {
749765
*r_is_valid = sys::conv::SYS_TRUE;
750766
}
751767

768+
#[cfg(debug_assertions)]
752769
pub unsafe extern "C" fn rust_callable_to_string_named<F>(
753770
callable_userdata: *mut std::ffi::c_void,
754771
r_is_valid: *mut sys::GDExtensionBool,

itest/rust/src/benchmarks/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use godot::builtin::inner::InnerRect2i;
1313
use godot::builtin::{GString, PackedInt32Array, Rect2i, StringName, Vector2i};
1414
use godot::classes::{Node3D, Os, RefCounted};
1515
use godot::obj::{Gd, InstanceId, NewAlloc, NewGd, Singleton};
16+
use godot::prelude::{varray, Callable, RustCallable, Variant};
1617
use godot::register::GodotClass;
1718

1819
use crate::framework::bench;
@@ -113,9 +114,36 @@ fn packed_array_from_iter_unknown_size() -> PackedInt32Array {
113114
}))
114115
}
115116

117+
#[bench(repeat = 25)]
118+
fn call_callv_rust_fn() -> Variant {
119+
let callable = Callable::from_fn("RustFunction", |_| ());
120+
callable.callv(&varray![])
121+
}
122+
123+
#[bench(repeat = 25)]
124+
fn call_callv_custom() -> Variant {
125+
let callable = Callable::from_custom(MyRustCallable {});
126+
callable.callv(&varray![])
127+
}
128+
116129
// ----------------------------------------------------------------------------------------------------------------------------------------------
117130
// Helpers for benchmarks above
118131

119132
#[derive(GodotClass)]
120133
#[class(init)]
121134
struct MyBenchType {}
135+
136+
#[derive(PartialEq, Hash)]
137+
struct MyRustCallable {}
138+
139+
impl RustCallable for MyRustCallable {
140+
fn invoke(&mut self, _args: &[&Variant]) -> Variant {
141+
Variant::nil()
142+
}
143+
}
144+
145+
impl std::fmt::Display for MyRustCallable {
146+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147+
write!(f, "MyRustCallable")
148+
}
149+
}

0 commit comments

Comments
 (0)