Refactor instantiate to return Closure
The `instantiate` method in `Environment` has been refactored to return `Result<Rc<Closure>, String>` instead of `Rc<NativeFunction>`. This change separates the creation of a `Closure` from its execution, allowing the caller to manage the `VM` lifecycle and choose the appropriate execution strategy (e.g., single run vs. repeated benchmark iterations). The `NativeFunction` type is now exclusively used for true Rust intrinsics. The benchmark runner has also been updated to reuse a single `VM` across iterations, improving performance by avoiding repeated VM allocations.
This commit is contained in:
+7
-6
@@ -55,7 +55,7 @@ pub fn run_functional_tests_with_optimization(enabled: bool) -> Vec<TestResult>
|
||||
results
|
||||
}
|
||||
|
||||
pub fn run_benchmarks(update: bool, filter: Option<&str>) -> Vec<BenchmarkResult> {
|
||||
pub fn ca(update: bool, filter: Option<&str>) -> Vec<BenchmarkResult> {
|
||||
let mut results = Vec::new();
|
||||
let entries = fs::read_dir("examples").unwrap();
|
||||
let is_release = !cfg!(debug_assertions);
|
||||
@@ -104,18 +104,19 @@ pub fn run_benchmarks(update: bool, filter: Option<&str>) -> Vec<BenchmarkResult
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to measure sum of VM execution times over N executions in one environment
|
||||
// Helper to measure sum of VM execution times over N executions in one environment.
|
||||
// A single VM is reused across iterations: run_with_args resets stack/frames
|
||||
// on each call, so no re-allocation is needed between runs.
|
||||
let measure_sum =
|
||||
|n: u32, node: &TypedNode| -> Result<Duration, String> {
|
||||
let env = Environment::new();
|
||||
|
||||
// Link once per sample
|
||||
let linked = env.link(node.clone());
|
||||
let func = env.instantiate(linked);
|
||||
let closure = env.instantiate(linked)?;
|
||||
let mut vm = env.create_vm();
|
||||
let mut total = Duration::ZERO;
|
||||
for _ in 0..n {
|
||||
let start = Instant::now();
|
||||
let _ = (func.func)(&[]);
|
||||
let _ = vm.run_with_args(closure.clone(), &[])?;
|
||||
total += start.elapsed();
|
||||
}
|
||||
Ok(total)
|
||||
|
||||
Reference in New Issue
Block a user