Skip to content

Commit ed78987

Browse files
jimpoVeykril
authored andcommitted
Remove allocation when calling functions
Instead of allocating an array of arg pointers, use the variable arguments signature m3_CallV.
1 parent dbbd72d commit ed78987

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

src/function.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::str;
77
use crate::error::{Error, Result};
88
use crate::runtime::Runtime;
99
use crate::utils::cstr_to_str;
10-
use crate::{Module, WasmArgs, WasmType};
10+
use crate::{Module, WasmArg, WasmArgs, WasmType};
1111

1212
/// Calling Context for a host function.
1313
pub struct CallContext<'cc> {
@@ -136,11 +136,7 @@ where
136136
})
137137
}
138138

139-
fn call_impl(&self, args: Args) -> Result<Ret> {
140-
let mut argv = args.ptrs_vec();
141-
let result =
142-
unsafe { ffi::m3_Call(self.raw.as_ptr(), argv.len() as u32, argv.as_mut_ptr()) };
143-
Error::from_ffi_res(result)?;
139+
fn get_call_result(&self) -> Result<Ret> {
144140
unsafe {
145141
let mut ret = core::mem::MaybeUninit::<Ret>::uninit();
146142
let result = ffi::m3_GetResultsV(self.raw.as_ptr(), ret.as_mut_ptr());
@@ -168,7 +164,9 @@ macro_rules! func_call_impl {
168164
#[inline]
169165
#[allow(non_snake_case, clippy::too_many_arguments)]
170166
pub fn call(&self, $($types: $types),*) -> Result<Ret> {
171-
self.call_impl(($($types,)*))
167+
let result = unsafe { ffi::m3_CallV(self.raw.as_ptr(), $($types,)*) };
168+
Error::from_ffi_res(result)?;
169+
self.get_call_result()
172170
}
173171
}
174172
};
@@ -178,13 +176,15 @@ func_call_impl!(A, B, C, D, E, F, G, H, J, K, L, M, N, O, P, Q);
178176
impl<'rt, ARG, Ret> Function<'rt, ARG, Ret>
179177
where
180178
Ret: WasmType,
181-
ARG: crate::WasmArg,
179+
ARG: WasmArg,
182180
{
183181
/// Calls this function with the given parameter.
184182
/// This is implemented with variable arguments depending on the functions Args type.
185183
#[inline]
186184
pub fn call(&self, arg: ARG) -> Result<Ret> {
187-
self.call_impl(arg)
185+
let result = unsafe { ffi::m3_CallV(self.raw.as_ptr(), arg) };
186+
Error::from_ffi_res(result)?;
187+
self.get_call_result()
188188
}
189189
}
190190

@@ -196,6 +196,8 @@ where
196196
/// This is implemented with variable arguments depending on the functions Args type.
197197
#[inline]
198198
pub fn call(&self) -> Result<Ret> {
199-
self.call_impl(())
199+
let result = unsafe { ffi::m3_CallV(self.raw.as_ptr()) };
200+
Error::from_ffi_res(result)?;
201+
self.get_call_result()
200202
}
201203
}

src/ty.rs

-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use alloc::vec;
21
use alloc::vec::Vec;
32

43
// this module looks like a mess, lots of doc(hidden) attributes since rust traits cant have private functions
@@ -39,8 +38,6 @@ pub trait WasmArgs {
3938
fn sealed_() -> private::Seal;
4039
#[doc(hidden)]
4140
fn append_signature(buffer: &mut Vec<cty::c_char>);
42-
#[doc(hidden)]
43-
fn ptrs_vec(&self) -> Vec<*const cty::c_void>;
4441
}
4542

4643
impl WasmArg for i32 {}
@@ -207,10 +204,6 @@ impl WasmArgs for () {
207204
}
208205
#[doc(hidden)]
209206
fn append_signature(_buffer: &mut Vec<cty::c_char>) {}
210-
#[doc(hidden)]
211-
fn ptrs_vec(&self) -> Vec<*const cty::c_void> {
212-
Vec::new()
213-
}
214207
}
215208

216209
/// Unary functions
@@ -238,10 +231,6 @@ where
238231
fn append_signature(buffer: &mut Vec<cty::c_char>) {
239232
buffer.push(T::SIGNATURE as cty::c_char);
240233
}
241-
#[doc(hidden)]
242-
fn ptrs_vec(&self) -> Vec<*const cty::c_void> {
243-
vec![self as *const T as *const cty::c_void]
244-
}
245234
}
246235

247236
macro_rules! args_impl {
@@ -292,12 +281,6 @@ macro_rules! args_impl {
292281
buffer.push($types::SIGNATURE as cty::c_char);
293282
)*
294283
}
295-
#[doc(hidden)]
296-
fn ptrs_vec(&self) -> Vec<*const cty::c_void> {
297-
#[allow(non_snake_case)]
298-
let ($($types,)*) = self;
299-
vec![$($types as *const $types as *const cty::c_void,)*]
300-
}
301284
}
302285
};
303286
}

0 commit comments

Comments
 (0)