Skip to content

Commit 5185056

Browse files
committed
Use optimal PackedFunc
1 parent 717d025 commit 5185056

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

rust/common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This crate contains the refactored basic components required
22
//! for `runtime` and `frontend` TVM crates.
33
4-
#![feature(box_syntax, try_from)]
4+
#![feature(box_syntax, trait_alias, try_from)]
55

66
#[macro_use]
77
extern crate failure;

rust/common/src/packed_func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use failure::Error;
55
pub use crate::ffi::TVMValue;
66
use crate::ffi::*;
77

8-
pub type PackedFunc = Box<Fn(&[TVMArgValue]) -> Result<TVMRetValue, crate::errors::FuncCallError>>;
8+
pub trait PackedFunc = Fn(&[TVMArgValue]) -> Result<TVMRetValue, crate::errors::FuncCallError> + Send + Sync;
99

1010
/// Calls a packed function and returns a `TVMRetValue`.
1111
///

rust/runtime/src/module.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ use tvm_common::{
88
};
99

1010
pub trait Module {
11-
fn get_function<S: AsRef<str>>(&self, name: S) -> Option<PackedFunc>;
11+
fn get_function<S: AsRef<str>>(&self, name: S) -> Option<&(dyn PackedFunc)>;
1212
}
1313

1414
pub struct SystemLibModule;
1515

1616
lazy_static! {
17-
static ref SYSTEM_LIB_FUNCTIONS: Mutex<HashMap<String, BackendPackedCFunc>> =
17+
static ref SYSTEM_LIB_FUNCTIONS: Mutex<HashMap<String, &'static (dyn PackedFunc)>> =
1818
Mutex::new(HashMap::new());
1919
}
2020

2121
impl Module for SystemLibModule {
22-
fn get_function<S: AsRef<str>>(&self, name: S) -> Option<PackedFunc> {
22+
fn get_function<S: AsRef<str>>(&self, name: S) -> Option<&(dyn PackedFunc)> {
2323
SYSTEM_LIB_FUNCTIONS
2424
.lock()
2525
.unwrap()
2626
.get(name.as_ref())
27-
.map(|func| wrap_backend_packed_func(name.as_ref().to_owned(), func.to_owned()))
27+
.map(|f| *f)
2828
}
2929
}
3030

@@ -35,8 +35,11 @@ impl Default for SystemLibModule {
3535
}
3636

3737
// @see `WrapPackedFunc` in `llvm_module.cc`.
38-
pub(super) fn wrap_backend_packed_func(func_name: String, func: BackendPackedCFunc) -> PackedFunc {
39-
box move |args: &[TVMArgValue]| {
38+
pub(super) fn wrap_backend_packed_func(
39+
func_name: String,
40+
func: BackendPackedCFunc,
41+
) -> Box<dyn PackedFunc> {
42+
Box::new(move |args: &[TVMArgValue]| {
4043
let exit_code = func(
4144
args.iter()
4245
.map(|ref arg| arg.value)
@@ -55,7 +58,7 @@ pub(super) fn wrap_backend_packed_func(func_name: String, func: BackendPackedCFu
5558
func_name.clone(),
5659
))
5760
}
58-
}
61+
})
5962
}
6063

6164
#[no_mangle]
@@ -64,9 +67,9 @@ pub extern "C" fn TVMBackendRegisterSystemLibSymbol(
6467
func: BackendPackedCFunc,
6568
) -> i32 {
6669
let name = unsafe { CStr::from_ptr(cname).to_str().unwrap() };
67-
SYSTEM_LIB_FUNCTIONS
68-
.lock()
69-
.unwrap()
70-
.insert(name.to_string(), func);
70+
SYSTEM_LIB_FUNCTIONS.lock().unwrap().insert(
71+
name.to_string(),
72+
&*Box::leak(wrap_backend_packed_func(name.to_string(), func)),
73+
);
7174
return 0;
7275
}

0 commit comments

Comments
 (0)