diff --git a/Cargo.lock b/Cargo.lock index 8bbb4716..7d7f8f9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3005,6 +3005,5 @@ name = "ziskos" version = "0.1.0" dependencies = [ "getrandom", - "lazy_static", "rand", ] diff --git a/Cargo.toml b/Cargo.toml index 14f9ed48..45de5558 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,3 +40,5 @@ p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9 log = "0.4" rayon = "1.10" num-bigint = "0.4" +rand = "0.8.5" +getrandom = { version = "0.2", features = ["custom"] } diff --git a/ziskos/entrypoint/Cargo.toml b/ziskos/entrypoint/Cargo.toml index 499ade31..ac31fee2 100644 --- a/ziskos/entrypoint/Cargo.toml +++ b/ziskos/entrypoint/Cargo.toml @@ -6,6 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lazy_static = "1.5.0" -rand = "0.8.5" -getrandom = { version = "0.2", features = ["custom"] } +rand.workspace = true +getrandom.workspace = true diff --git a/ziskos/entrypoint/src/lib.rs b/ziskos/entrypoint/src/lib.rs index c7f8f472..192f198a 100644 --- a/ziskos/entrypoint/src/lib.rs +++ b/ziskos/entrypoint/src/lib.rs @@ -22,25 +22,51 @@ macro_rules! entrypoint { use crate::ziskos_definitions::ziskos_config::*; #[cfg(target_os = "ziskos")] -pub fn read_input() -> Vec { +pub fn read_input<'a>() -> &'a [u8] { // Create a slice of the first 8 bytes to get the size let bytes = unsafe { core::slice::from_raw_parts(INPUT_ADDR as *const u8, 8) }; // Convert the slice to a u64 (little-endian) let size: u64 = u64::from_le_bytes(bytes.try_into().unwrap()); - let input = - unsafe { core::slice::from_raw_parts((INPUT_ADDR as *const u8).add(8), size as usize) }; - input.to_vec() + unsafe { core::slice::from_raw_parts((INPUT_ADDR as *const u8).add(8), size as usize) } } #[cfg(not(target_os = "ziskos"))] -pub fn read_input() -> Vec { - use std::{fs::File, io::Read}; +use std::cell::RefCell; - let mut file = File::open("build/input.bin").unwrap(); - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer).unwrap(); - buffer +#[cfg(not(target_os = "ziskos"))] +thread_local! { + /// A thread-local storage to memoize the `build/input.bin` file + static FILE_CONTENT: RefCell> = RefCell::new(None); +} + +#[cfg(not(target_os = "ziskos"))] +pub fn read_input<'a>() -> &'static [u8] { + use std::{ + fs::File, + io::{Error, Read}, + }; + FILE_CONTENT + .with(|file_content| { + // Return early if we have already memoized the file + if let Some(static_slice) = *file_content.borrow() { + return Ok::<&'static [u8], Error>(static_slice); + } + + // Read the file into a Vec + let mut file = File::open("build/input.bin")?; + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer)?; + + // Leak the memory to get a 'static reference + let static_slice: &'static [u8] = Box::leak(buffer.into_boxed_slice()); + + // Memoize the result in thread-local storage + *file_content.borrow_mut() = Some(static_slice); + + Ok(static_slice) + }) + .expect("Failed to read 'build/input.bin'") } #[cfg(target_os = "ziskos")]