Skip to content

perf: reduces size of ScriptValue to 64 bytes, moves some dynamic function methods into function info #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ impl FunctionCallContext {
}
}

#[derive(Clone, Reflect)]
#[derive(Reflect, Clone)]
#[reflect(opaque)]
/// A dynamic script function.
pub struct DynamicScriptFunction {
/// The meta information about the function
pub info: FunctionInfo,
pub info: Arc<FunctionInfo>,
// TODO: info about the function, this is hard right now because of non 'static lifetimes in wrappers, we can't use TypePath etc
func: Arc<
dyn Fn(FunctionCallContext, VecDeque<ScriptValue>) -> ScriptValue + Send + Sync + 'static,
>,
}

#[derive(Clone, Reflect)]
#[derive(Reflect, Clone)]
#[reflect(opaque)]
/// A dynamic mutable script function.
pub struct DynamicScriptFunctionMut {
/// The meta information about the function
pub info: FunctionInfo,
pub info: Arc<FunctionInfo>,
func: Arc<
RwLock<
// I'd rather consume an option or something instead of having the RWLock but I just wanna get this release out
Expand Down Expand Up @@ -129,30 +129,9 @@ impl DynamicScriptFunction {
}

/// Set the meta information about the function
pub fn with_info(self, info: FunctionInfo) -> Self {
Self { info, ..self }
}

/// Set the name of the function
pub fn with_name<N: Into<Cow<'static, str>>>(self, name: N) -> Self {
Self {
info: FunctionInfo {
name: name.into(),
..self.info
},
func: self.func,
}
}

/// Set the namespace of the function
pub fn with_namespace(self, namespace: Namespace) -> Self {
Self {
info: FunctionInfo {
namespace,
..self.info
},
func: self.func,
}
pub fn with_info(mut self, info: FunctionInfo) -> Self {
self.info = Arc::new(info);
self
}
}

Expand Down Expand Up @@ -187,30 +166,9 @@ impl DynamicScriptFunctionMut {
}

/// Set the meta information about the function
pub fn with_info(self, info: FunctionInfo) -> Self {
Self { info, ..self }
}

/// Set the name of the function
pub fn with_name<N: Into<Cow<'static, str>>>(self, name: N) -> Self {
Self {
info: FunctionInfo {
name: name.into(),
..self.info
},
func: self.func,
}
}

/// Set the namespace of the function
pub fn with_namespace(self, namespace: Namespace) -> Self {
Self {
info: FunctionInfo {
namespace,
..self.info
},
func: self.func,
}
pub fn with_info(mut self, info: FunctionInfo) -> Self {
self.info = Arc::new(info);
self
}
}

Expand Down Expand Up @@ -248,10 +206,11 @@ where
{
fn from(fn_: F) -> Self {
DynamicScriptFunction {
info: FunctionInfo::default(),
info: FunctionInfo::default()
.with_name(std::any::type_name::<F>())
.into(),
func: Arc::new(fn_),
}
.with_name(std::any::type_name::<F>())
}
}

Expand All @@ -261,10 +220,11 @@ where
{
fn from(fn_: F) -> Self {
DynamicScriptFunctionMut {
info: FunctionInfo::default(),
info: FunctionInfo::default()
.with_name(std::any::type_name::<F>())
.into(),
func: Arc::new(RwLock::new(fn_)),
}
.with_name(std::any::type_name::<F>())
}
}

Expand Down Expand Up @@ -711,7 +671,7 @@ mod test {
#[test]
fn test_invalid_amount_of_args_errors_nicely() {
let fn_ = |a: usize, b: usize| a + b;
let script_function = fn_.into_dynamic_script_function().with_name("my_fn");
let script_function = fn_.into_dynamic_script_function();

with_local_world(|| {
let out = script_function.call(
Expand All @@ -723,7 +683,7 @@ mod test {
assert_eq!(
out.unwrap_err(),
InteropError::function_interop_error(
"my_fn",
"<bevy_mod_scripting_core::bindings::function::script_function::test::test_invalid_amount_of_args_errors_nicely::{{closure}} as bevy_mod_scripting_core::bindings::function::script_function::ScriptFunction<fn(usize, usize) -> usize>>::into_dynamic_script_function::{{closure}}",
Namespace::Global,
InteropError::argument_count_mismatch(2, 1)
)
Expand All @@ -737,7 +697,7 @@ mod test {
struct Comp;

let fn_ = |_a: crate::bindings::function::from::Mut<Comp>| 0usize;
let script_function = fn_.into_dynamic_script_function().with_name("my_fn");
let script_function = fn_.into_dynamic_script_function();

with_local_world(|| {
let out = script_function.call(
Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_mod_scripting_core/src/docgen/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ impl FunctionInfo {
}
}

/// Set the name of the function info.
pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
self.name = name.into();
self
}

/// Set the namespace of the function info.
pub fn with_namespace(mut self, namespace: Namespace) -> Self {
self.namespace = namespace;
self
}

/// Add an argument to the function info.
pub fn add_arg<T: ArgMeta + TypedThrough + 'static>(
mut self,
Expand Down
Loading