Skip to content

Commit

Permalink
Remove panics in cache (#902)
Browse files Browse the repository at this point in the history
* Add lints

* Remove panic from AotExecutor

* Remove panic from jit

* Add panic lint

* Add comment

* Remove lint

* Fix clippy
  • Loading branch information
JulianGCalderon authored Nov 13, 2024
1 parent 4d24686 commit d743d04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
26 changes: 18 additions & 8 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 13 additions & 12 deletions src/cache/aot.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -39,39 +40,38 @@ where
key: K,
program: &Program,
opt_level: OptLevel,
) -> Arc<AotNativeExecutor> {
) -> Result<Arc<AotNativeExecutor>> {
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::<GasMetadata>().cloned().unwrap(),
metadata
.get::<GasMetadata>()
.cloned()
.ok_or(Error::MissingMetadata)?,
);

let executor = Arc::new(executor);
self.cache.insert(key, executor.clone());

executor
Ok(executor)
}
}

Expand Down Expand Up @@ -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");

Expand Down
18 changes: 10 additions & 8 deletions src/cache/jit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::Result;
use crate::{context::NativeContext, executor::JitNativeExecutor, OptLevel};
use cairo_lang_sierra::program::Program;
use std::{
Expand Down Expand Up @@ -44,17 +45,14 @@ where
key: K,
program: &Program,
opt_level: OptLevel,
) -> Arc<JitNativeExecutor<'a>> {
let module = self
.context
.compile(program, false)
.expect("should compile");
) -> Result<Arc<JitNativeExecutor<'a>>> {
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)
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit d743d04

Please sign in to comment.