diff --git a/benches/benches.rs b/benches/benches.rs index fce64713b..eca0d396b 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -21,15 +21,25 @@ fn criterion_benchmark(c: &mut Criterion) { let fibonacci = load_contract("programs/benches/fib_2M.cairo"); let logistic_map = load_contract("programs/benches/logistic_map.cairo"); - let aot_factorial = aot_cache.compile_and_insert(Felt::ZERO, &factorial, OptLevel::Aggressive); - let aot_fibonacci = aot_cache.compile_and_insert(Felt::ONE, &fibonacci, OptLevel::Aggressive); - let aot_logistic_map = - aot_cache.compile_and_insert(Felt::from(2), &logistic_map, OptLevel::Aggressive); + let aot_factorial = aot_cache + .compile_and_insert(Felt::ZERO, &factorial, OptLevel::Aggressive) + .unwrap(); + let aot_fibonacci = aot_cache + .compile_and_insert(Felt::ONE, &fibonacci, OptLevel::Aggressive) + .unwrap(); + let aot_logistic_map = aot_cache + .compile_and_insert(Felt::from(2), &logistic_map, OptLevel::Aggressive) + .unwrap(); - let jit_factorial = jit_cache.compile_and_insert(Felt::ZERO, &factorial, OptLevel::Aggressive); - let jit_fibonacci = jit_cache.compile_and_insert(Felt::ONE, &fibonacci, OptLevel::Aggressive); - let jit_logistic_map = - jit_cache.compile_and_insert(Felt::from(2), &logistic_map, OptLevel::Aggressive); + let jit_factorial = jit_cache + .compile_and_insert(Felt::ZERO, &factorial, OptLevel::Aggressive) + .unwrap(); + let jit_fibonacci = jit_cache + .compile_and_insert(Felt::ONE, &fibonacci, OptLevel::Aggressive) + .unwrap(); + let jit_logistic_map = jit_cache + .compile_and_insert(Felt::from(2), &logistic_map, OptLevel::Aggressive) + .unwrap(); let factorial_function_id = find_function_id(&factorial, "factorial_2M::factorial_2M::main").unwrap(); diff --git a/src/cache/aot.rs b/src/cache/aot.rs index 392c72484..8c77ae7e7 100644 --- a/src/cache/aot.rs +++ b/src/cache/aot.rs @@ -1,3 +1,4 @@ +use crate::error::{Error, Result}; use crate::{ context::NativeContext, executor::AotNativeExecutor, metadata::gas::GasMetadata, module::NativeModule, utils::SHARED_LIBRARY_EXT, OptLevel, @@ -39,39 +40,38 @@ where key: K, program: &Program, opt_level: OptLevel, - ) -> Arc { + ) -> Result> { let NativeModule { module, registry, metadata, - } = self - .context - .compile(program, false) - .expect("should compile"); + } = self.context.compile(program, false)?; // Compile module into an object. - let object_data = crate::ffi::module_to_object(&module, opt_level).unwrap(); + let object_data = crate::ffi::module_to_object(&module, opt_level)?; // Compile object into a shared library. let shared_library_path = tempfile::Builder::new() .prefix("lib") .suffix(SHARED_LIBRARY_EXT) - .tempfile() - .unwrap() + .tempfile()? .into_temp_path(); - crate::ffi::object_to_shared_lib(&object_data, &shared_library_path).unwrap(); + crate::ffi::object_to_shared_lib(&object_data, &shared_library_path)?; - let shared_library = unsafe { Library::new(shared_library_path).unwrap() }; + let shared_library = unsafe { Library::new(shared_library_path)? }; let executor = AotNativeExecutor::new( shared_library, registry, - metadata.get::().cloned().unwrap(), + metadata + .get::() + .cloned() + .ok_or(Error::MissingMetadata)?, ); let executor = Arc::new(executor); self.cache.insert(key, executor.clone()); - executor + Ok(executor) } } @@ -104,6 +104,7 @@ mod tests { let function_id = &program.funcs.first().expect("should have a function").id; let executor = cache.compile_and_insert((), &program, OptLevel::default()); let res = executor + .unwrap() .invoke_dynamic(function_id, &[], Some(u128::MAX)) .expect("should run"); diff --git a/src/cache/jit.rs b/src/cache/jit.rs index d575db047..223f4ba35 100644 --- a/src/cache/jit.rs +++ b/src/cache/jit.rs @@ -1,3 +1,4 @@ +use crate::error::Result; use crate::{context::NativeContext, executor::JitNativeExecutor, OptLevel}; use cairo_lang_sierra::program::Program; use std::{ @@ -44,17 +45,14 @@ where key: K, program: &Program, opt_level: OptLevel, - ) -> Arc> { - let module = self - .context - .compile(program, false) - .expect("should compile"); + ) -> Result>> { + let module = self.context.compile(program, false)?; let executor = JitNativeExecutor::from_native_module(module, opt_level); let executor = Arc::new(executor); self.cache.insert(key, executor.clone()); - executor + Ok(executor) } } @@ -91,7 +89,9 @@ mod test { let mut cache: JitProgramCache<&'static str> = JitProgramCache::new(&context); let start = Instant::now(); - cache.compile_and_insert("program1", &program1, Default::default()); + cache + .compile_and_insert("program1", &program1, Default::default()) + .unwrap(); let diff_1 = Instant::now().duration_since(start); let start = Instant::now(); @@ -101,7 +101,9 @@ mod test { assert!(diff_2 < diff_1); let start = Instant::now(); - cache.compile_and_insert("program2", &program2, Default::default()); + cache + .compile_and_insert("program2", &program2, Default::default()) + .unwrap(); let diff_1 = Instant::now().duration_since(start); let start = Instant::now();