Skip to content

Commit 9cd4d65

Browse files
authored
Merge pull request #1331 from lyonbeckers/debug-callcontext
Only provide function name to CallContext in debug
2 parents 2100dbf + c98a0c2 commit 9cd4d65

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

godot-core/src/builtin/callable.rs

Lines changed: 26 additions & 5 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()
@@ -608,6 +617,7 @@ mod custom_callable {
608617

609618
pub(crate) struct FnWrapper<F> {
610619
pub(super) rust_function: F,
620+
#[cfg(debug_assertions)]
611621
pub(super) name: GString,
612622

613623
/// `None` if the callable is multi-threaded ([`Callable::from_sync_fn`]).
@@ -658,11 +668,14 @@ mod custom_callable {
658668
) {
659669
let arg_refs: &[&Variant] = Variant::borrow_ref_slice(p_args, p_argument_count as usize);
660670

661-
let name = {
671+
#[cfg(debug_assertions)]
672+
let name = &{
662673
let c: &C = CallableUserdata::inner_from_raw(callable_userdata);
663674
c.to_string()
664675
};
665-
let ctx = meta::CallContext::custom_callable(name.as_str());
676+
#[cfg(not(debug_assertions))]
677+
let name = "<optimized out>";
678+
let ctx = meta::CallContext::custom_callable(name);
666679

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

688-
let name = {
701+
#[cfg(debug_assertions)]
702+
let name = &{
689703
let w: &FnWrapper<F> = CallableUserdata::inner_from_raw(callable_userdata);
690704
w.name.to_string()
691705
};
692-
let ctx = meta::CallContext::custom_callable(name.as_str());
706+
#[cfg(not(debug_assertions))]
707+
let name = "<optimized out>";
708+
let ctx = meta::CallContext::custom_callable(name);
693709

694710
crate::private::handle_varcall_panic(&ctx, &mut *r_error, move || {
695711
// Get the FnWrapper again inside closure so the FnMut doesn't have to be UnwindSafe.
@@ -698,12 +714,16 @@ mod custom_callable {
698714
if w.thread_id
699715
.is_some_and(|tid| tid != std::thread::current().id())
700716
{
717+
#[cfg(debug_assertions)]
718+
let name = &w.name;
719+
#[cfg(not(debug_assertions))]
720+
let name = "<optimized out>";
701721
// NOTE: this panic is currently not propagated to the caller, but results in an error message and Nil return.
702722
// See comments in itest callable_call() for details.
703723
panic!(
704724
"Callable '{}' created with from_fn() must be called from the same thread it was created in.\n\
705725
If you need to call it from any thread, use from_sync_fn() instead (requires `experimental-threads` feature).",
706-
w.name
726+
name
707727
);
708728
}
709729

@@ -749,6 +769,7 @@ mod custom_callable {
749769
*r_is_valid = sys::conv::SYS_TRUE;
750770
}
751771

772+
#[cfg(debug_assertions)]
752773
pub unsafe extern "C" fn rust_callable_to_string_named<F>(
753774
callable_userdata: *mut std::ffi::c_void,
754775
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)