Skip to content

Commit

Permalink
Implement host functions in 1 case, optimize NativeFunc::call
Browse files Browse the repository at this point in the history
`NativeFunc::call` is about 8% faster in the case we're benchmarking
by avoiding allocating a vector for the params / returns, instead we
do logic to determine which is larger and use that, conditionally
copying it back to the rets array if needed.
  • Loading branch information
Mark McCaskey committed Jun 9, 2020
1 parent 85b901d commit eb928e7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
1 change: 0 additions & 1 deletion benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::sync::Arc;
use test_utils::{get_compiler_config_from_str, wasmer_compilers};
use wasmer::*;
use wasmer_engine_jit::JITEngine;
Expand Down
90 changes: 54 additions & 36 deletions lib/api/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::marker::PhantomData;

use crate::exports::{ExportError, Exportable};
use crate::externals::function::{FunctionDefinition, WasmFunctionDefinition};
use crate::{Extern, Function, FunctionType, Store};
use crate::{Extern, Function, FunctionType, RuntimeError, Store};
use wasm_common::{NativeWasmType, WasmExternType, WasmTypeList};
use wasmer_runtime::{
wasmer_call_trampoline, Export, ExportFunction, VMContext, VMFunctionBody, VMFunctionKind,
Expand All @@ -27,7 +27,11 @@ pub struct NativeFunc<'a, Args = UnprovidedArgs, Rets = UnprovidedRets> {

unsafe impl<'a, Args, Rets> Send for NativeFunc<'a, Args, Rets> {}

impl<'a, Args, Rets> NativeFunc<'a, Args, Rets> {
impl<'a, Args, Rets> NativeFunc<'a, Args, Rets>
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
pub(crate) fn new(
store: Store,
address: *const VMFunctionBody,
Expand Down Expand Up @@ -115,56 +119,70 @@ macro_rules! impl_native_traits {
Rets: WasmTypeList,
{
/// Call the typed func and return results.
pub fn call(&self, $( $x: $x, )* ) -> Result<Rets, ()> {
let params = [ $( $x.to_native().to_binary() ),* ];
let mut values_vec: Vec<i128> = vec![0; std::cmp::max(params.len(), Rets::wasm_types().len())];

for (i, &arg) in params.iter().enumerate() {
values_vec[i] = arg;
}
pub fn call(&self, $( $x: $x, )* ) -> Result<Rets, RuntimeError> {
// TODO: when `const fn` related features mature more, we can declare a single array
// of the correct size here.
let mut params_list = [ $( $x.to_native().to_binary() ),* ];
let mut rets_list_array = Rets::empty_array();
let rets_list = rets_list_array.as_mut();
let using_rets_array;
let args_rets: &mut [i128] = if params_list.len() > rets_list.len() {
using_rets_array = false;
params_list.as_mut()
} else {
using_rets_array = true;
for (i, &arg) in params_list.iter().enumerate() {
rets_list[i] = arg;
}
rets_list.as_mut()
};

match self.definition {
FunctionDefinition::Wasm(WasmFunctionDefinition {
trampoline
}) => {
if let Err(error) = unsafe {
unsafe {
wasmer_call_trampoline(
self.vmctx,
trampoline,
self.address,
values_vec.as_mut_ptr() as *mut u8,
args_rets.as_mut_ptr() as *mut u8,
)
} {
dbg!(error);
return Err(());
} else {
let mut results = Rets::empty_array();
let num_results = Rets::wasm_types().len();
if num_results > 0 {
unsafe {
std::ptr::copy_nonoverlapping(values_vec.as_ptr(),
&mut results.as_mut()[0] as *mut i128,
num_results);
}
}?;
let num_rets = rets_list.len();
if !using_rets_array && num_rets > 0 {
let src_pointer = params_list.as_ptr();
let rets_list = &mut rets_list_array.as_mut()[0] as *mut i128;
unsafe {
// TODO: we can probably remove this copy by doing some clever `transmute`s.
// we know it's not overlapping because `using_rets_array` is false
std::ptr::copy_nonoverlapping(src_pointer,
rets_list,
num_rets);
}
return Ok(Rets::from_array(results));
}
return Ok(Rets::from_array(rets_list_array));
}
FunctionDefinition::Host => {
/*unsafe {
let f = std::mem::transmute::<_, unsafe extern "C" fn( *mut VMContext, $( $x, )*) -> Result<Rets, RuntimeError>>(self.address);
match f( self.vmctx, $( $x, )* ) {
Err(error) => {
dbg!(error);
return Err(());
}
Ok(results) => {
return Ok(results);
}
if self.arg_kind == VMFunctionKind::Static {
unsafe {
let f = std::mem::transmute::<_, unsafe fn( $( $x, )*) -> Rets>(self.address);

let results = f( $( $x, )* );
return Ok(results);
/* match f( $( $x, )* ) {
Err(error) => {
dbg!(error);
return Err(());
}
Ok(results) => {
return Ok(results);
}
}*/
}
} else {
todo!("dynamic host functions not yet implemented")
}
*/
todo!("host functions not yet implemented")
},
}

Expand Down
1 change: 0 additions & 1 deletion lib/api/tests/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ fn function_new_dynamic_env() -> Result<()> {
}

// TODO: unignore this when calling host functions has been implemented
#[ignore]
#[test]
fn native_function_works() -> Result<()> {
let store = Store::default();
Expand Down

0 comments on commit eb928e7

Please sign in to comment.