Skip to content

Commit

Permalink
improvement(perf): separate StructRef and RootedStruct
Browse files Browse the repository at this point in the history
Mun structs are marshalled as a StructRef. To make them rooted, they
need to be converted to a RootedStruct. This avoids needing to reacquire
the shared reference when getting, setting, and replacing struct fields
  • Loading branch information
Wodann committed Jul 17, 2020
1 parent 686fddb commit 16d5812
Show file tree
Hide file tree
Showing 20 changed files with 720 additions and 417 deletions.
11 changes: 7 additions & 4 deletions book/listings/ch01-getting-started/listing04.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder};
use mun_runtime::{invoke_fn, RuntimeBuilder};
use std::{cell::RefCell, env, rc::Rc};

fn main() {
Expand All @@ -9,9 +9,12 @@ fn main() {
.expect("Failed to spawn Runtime");

loop {
let arg: i64 = invoke_fn!(runtime, "arg").wait();
let result: i64 = invoke_fn!(runtime, "fibonacci", arg).wait();
println!("fibonacci({}) = {}", arg, result);
{
let runtime_ref = runtime.borrow();
let arg: i64 = invoke_fn!(runtime_ref, "arg").unwrap();
let result: i64 = invoke_fn!(runtime_ref, "fibonacci", arg).unwrap();
println!("fibonacci({}) = {}", arg, result);
}
runtime.borrow_mut().update();
}
}
5 changes: 3 additions & 2 deletions book/listings/ch02-basic-concepts/listing02.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder};
use mun_runtime::{invoke_fn, RuntimeBuilder};
use std::{cell::RefCell, rc::Rc};

fn main() {
let runtime = RuntimeBuilder::new("main.munlib")
.spawn()
.expect("Failed to spawn Runtime");

let result: bool = invoke_fn!(runtime, "random_bool").unwrap();
let runtime_ref = runtime.borrow();
let result: bool = invoke_fn!(runtime_ref, "random_bool").unwrap();
println!("random bool: {}", result);
}
5 changes: 3 additions & 2 deletions book/listings/ch02-basic-concepts/listing03.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder};
use mun_runtime::{invoke_fn, RuntimeBuilder};
use std::{cell::RefCell, rc::Rc};

extern "C" fn random() -> i64 {
Expand All @@ -13,6 +13,7 @@ fn main() {
.spawn()
.expect("Failed to spawn Runtime");

let result: bool = invoke_fn!(runtime, "random_bool").unwrap();
let runtime_ref = runtime.borrow();
let result: bool = invoke_fn!(runtime_ref, "random_bool").unwrap();
println!("random_bool: {}", result);
}
7 changes: 4 additions & 3 deletions book/listings/ch03-structs/listing11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fn main() {
.spawn()
.expect("Failed to spawn Runtime");

let a: StructRef = invoke_fn!(runtime, "vector2_new", -1.0f32, 1.0f32).unwrap();
let b: StructRef = invoke_fn!(runtime, "vector2_new", 1.0f32, -1.0f32).unwrap();
let added: StructRef = invoke_fn!(runtime, "vector2_add", a, b).unwrap();
let runtime_ref = runtime.borrow();
let a: StructRef = invoke_fn!(runtime_ref, "vector2_new", -1.0f32, 1.0f32).unwrap();
let b: StructRef = invoke_fn!(runtime_ref, "vector2_new", 1.0f32, -1.0f32).unwrap();
let added: StructRef = invoke_fn!(runtime_ref, "vector2_add", a, b).unwrap();
}
3 changes: 2 additions & 1 deletion book/listings/ch03-structs/listing12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# .spawn()
# .expect("Failed to spawn Runtime");
#
let mut xy: StructRef = invoke_fn!(runtime, "vector2_new", -1.0f32, 1.0f32).unwrap();
let runtime_ref = runtime.borrow();
let mut xy: StructRef = invoke_fn!(runtime_ref, "vector2_new", -1.0f32, 1.0f32).unwrap();
let x: f32 = xy.get("x").unwrap();
xy.set("x", x * x).unwrap();
let y = xy.replace("y", -1.0f32).unwrap();
Expand Down
13 changes: 10 additions & 3 deletions book/listings/ch03-structs/listing14.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# extern crate mun_runtime;
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder, StructRef};
use mun_runtime::{invoke_fn, RuntimeBuilder, StructRef};
use std::{env, time};

extern "C" fn log_f32(value: f32) {
Expand All @@ -14,7 +14,11 @@ fn main() {
.spawn()
.expect("Failed to spawn Runtime");

let ctx: StructRef = invoke_fn!(runtime, "new_sim").wait();
let ctx = {
let runtime_ref = runtime.borrow();
let ctx: StructRef = invoke_fn!(runtime_ref, "new_sim").unwrap();
ctx.root(runtime.clone())
};

let mut previous = time::Instant::now();
const FRAME_TIME: time::Duration = time::Duration::from_millis(40);
Expand All @@ -29,7 +33,10 @@ fn main() {
elapsed.as_secs_f32()
};

let _: () = invoke_fn!(runtime, "sim_update", ctx.clone(), elapsed_secs).wait();
{
let runtime_ref = runtime.borrow();
let _: () = invoke_fn!(runtime_ref, "sim_update", unsafe { ctx.as_ref(&runtime_ref) }, elapsed_secs).unwrap();
}
previous = now;

runtime.borrow_mut().update();
Expand Down
8 changes: 4 additions & 4 deletions crates/mun/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
if let Some(ret_type) = fn_definition.prototype.signature.return_type() {
let type_guid = &ret_type.guid;
if *type_guid == bool::type_guid() {
let result: bool = invoke_fn!(runtime, entry_point).map_err(|e| anyhow!("{}", e))?;
let result: bool = invoke_fn!(borrowed, entry_point).map_err(|e| anyhow!("{}", e))?;

println!("{}", result)
} else if *type_guid == f64::type_guid() {
let result: f64 = invoke_fn!(runtime, entry_point).map_err(|e| anyhow!("{}", e))?;
let result: f64 = invoke_fn!(borrowed, entry_point).map_err(|e| anyhow!("{}", e))?;

println!("{}", result)
} else if *type_guid == i64::type_guid() {
let result: i64 = invoke_fn!(runtime, entry_point).map_err(|e| anyhow!("{}", e))?;
let result: i64 = invoke_fn!(borrowed, entry_point).map_err(|e| anyhow!("{}", e))?;

println!("{}", result)
} else {
Expand All @@ -201,7 +201,7 @@ fn start(matches: &ArgMatches) -> Result<ExitStatus, anyhow::Error> {
Ok(ExitStatus::Success)
} else {
#[allow(clippy::unit_arg)]
invoke_fn!(runtime, entry_point)
invoke_fn!(borrowed, entry_point)
.map(|_: ()| ExitStatus::Success)
.map_err(|e| anyhow!("{}", e))
}
Expand Down
3 changes: 2 additions & 1 deletion crates/mun/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn build_and_run() {
assert!(library_path.is_file());

let runtime = RuntimeBuilder::new(&library_path).spawn().unwrap();
let result: i32 = invoke_fn!(runtime, "main").unwrap();
let runtime_ref = runtime.borrow();
let result: i32 = invoke_fn!(runtime_ref, "main").unwrap();
assert_eq!(result, TEST_VAL);
}
16 changes: 10 additions & 6 deletions crates/mun_runtime/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub fn fibonacci_benchmark(c: &mut Criterion) {
for i in [100i64, 200i64, 500i64, 1000i64, 4000i64, 8000i64].iter() {
// Run Mun fibonacci
group.bench_with_input(BenchmarkId::new("mun", i), i, |b, i| {
let runtime_ref = runtime.borrow();
b.iter(|| {
let _: i64 = invoke_fn!(runtime, "main", *i).unwrap();
let _: i64 = invoke_fn!(runtime_ref, "main", *i).unwrap();
})
});

Expand Down Expand Up @@ -77,8 +78,9 @@ pub fn empty_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("empty");

group.bench_function("mun", |b| {
let runtime_ref = runtime.borrow();
b.iter(|| {
let _: i64 = invoke_fn!(runtime, "empty", black_box(20i64)).unwrap();
let _: i64 = invoke_fn!(runtime_ref, "empty", black_box(20i64)).unwrap();
})
});
group.bench_function("rust", |b| b.iter(|| empty(black_box(20))));
Expand Down Expand Up @@ -112,8 +114,9 @@ struct RustParent<'a> {
pub fn get_struct_field_benchmark(c: &mut Criterion) {
// Perform setup (not part of the benchmark)
let runtime = util::runtime_from_file("struct.mun");
let mun_gc_parent: StructRef = invoke_fn!(runtime, "make_gc_parent").unwrap();
let mun_value_parent: StructRef = invoke_fn!(runtime, "make_value_parent").unwrap();
let runtime_ref = runtime.borrow_mut();
let mun_gc_parent: StructRef = invoke_fn!(runtime_ref, "make_gc_parent").unwrap();
let mun_value_parent: StructRef = invoke_fn!(runtime_ref, "make_value_parent").unwrap();

let rust_child = RustChild(-2.0, -1.0, 1.0, 2.0);
let rust_parent = RustParent {
Expand Down Expand Up @@ -200,8 +203,9 @@ pub fn get_struct_field_benchmark(c: &mut Criterion) {
pub fn set_struct_field_benchmark(c: &mut Criterion) {
// Perform setup (not part of the benchmark)
let runtime = util::runtime_from_file("struct.mun");
let mut mun_gc_parent: StructRef = invoke_fn!(runtime, "make_gc_parent").unwrap();
let mut mun_value_parent: StructRef = invoke_fn!(runtime, "make_value_parent").unwrap();
let runtime_ref = runtime.borrow();
let mut mun_gc_parent: StructRef = invoke_fn!(runtime_ref, "make_gc_parent").unwrap();
let mut mun_value_parent: StructRef = invoke_fn!(runtime_ref, "make_value_parent").unwrap();

let rust_child = RustChild(-2.0, -1.0, 1.0, 2.0);
let mut rust_child2 = rust_child.clone();
Expand Down
8 changes: 5 additions & 3 deletions crates/mun_runtime/examples/buoyancy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder, StructRef};
use mun_runtime::{invoke_fn, RuntimeBuilder, StructRef};
use std::{env, time};

extern "C" fn log_f32(value: f32) {
Expand All @@ -17,7 +17,8 @@ fn main() {
.spawn()
.expect("Failed to spawn Runtime");

let ctx: StructRef = invoke_fn!(runtime, "new_sim").wait();
let runtime_ref = runtime.borrow();
let ctx: StructRef = invoke_fn!(runtime_ref, "new_sim").unwrap();

let mut previous = time::Instant::now();
const FRAME_TIME: time::Duration = time::Duration::from_millis(40);
Expand All @@ -32,7 +33,8 @@ fn main() {
elapsed.as_secs_f32()
};

let _: () = invoke_fn!(runtime, "sim_update", ctx.clone(), elapsed_secs).wait();
let runtime_ref = runtime.borrow();
let _: () = invoke_fn!(runtime_ref, "sim_update", ctx.clone(), elapsed_secs).unwrap();
previous = now;

runtime.borrow_mut().update();
Expand Down
11 changes: 7 additions & 4 deletions crates/mun_runtime/examples/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mun_runtime::{invoke_fn, RetryResultExt, RuntimeBuilder};
use mun_runtime::{invoke_fn, RuntimeBuilder};
use std::env;

// How to run?
Expand All @@ -13,10 +13,13 @@ fn main() {
.spawn()
.expect("Failed to spawn Runtime");

let mut runtime_ref = runtime.borrow_mut();

loop {
let n: i64 = invoke_fn!(runtime, "nth").wait();
let result: i64 = invoke_fn!(runtime, "fibonacci", n).wait();
let n: i64 = invoke_fn!(runtime_ref, "nth").unwrap_or_else(|e| e.wait(&mut runtime_ref));
let result: i64 =
invoke_fn!(runtime_ref, "fibonacci", n).unwrap_or_else(|e| e.wait(&mut runtime_ref));
println!("fibonacci({}) = {}", n, result);
runtime.borrow_mut().update();
runtime_ref.update();
}
}
Loading

0 comments on commit 16d5812

Please sign in to comment.