From 86a39e0f7517aebf0b7a97570ed51eadf85cf7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Antu=C3=B1a=20D=C3=ADez?= Date: Mon, 21 Oct 2024 10:15:56 +0000 Subject: [PATCH 1/3] Refactor read_input function to return a borrowed slice instead of a vector --- ziskos/entrypoint/src/lib.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ziskos/entrypoint/src/lib.rs b/ziskos/entrypoint/src/lib.rs index c7f8f472..4633d1c2 100644 --- a/ziskos/entrypoint/src/lib.rs +++ b/ziskos/entrypoint/src/lib.rs @@ -22,25 +22,32 @@ 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}; +pub fn read_input<'a>() -> &'a [u8] { + use std::fs::File; + use std::io::Read; + use std::mem; + use std::slice; let mut file = File::open("build/input.bin").unwrap(); let mut buffer = Vec::new(); file.read_to_end(&mut buffer).unwrap(); - buffer + + let ptr = buffer.as_ptr(); + let len = buffer.len(); + + mem::forget(buffer); + + unsafe { slice::from_raw_parts(ptr, len) } } #[cfg(target_os = "ziskos")] From c0d9a6838dcc4e2278862f464245434d2c7e2ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Antu=C3=B1a=20D=C3=ADez?= Date: Mon, 21 Oct 2024 10:18:57 +0000 Subject: [PATCH 2/3] fix fmt --- ziskos/entrypoint/src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ziskos/entrypoint/src/lib.rs b/ziskos/entrypoint/src/lib.rs index 4633d1c2..5a7e62be 100644 --- a/ziskos/entrypoint/src/lib.rs +++ b/ziskos/entrypoint/src/lib.rs @@ -33,10 +33,7 @@ pub fn read_input<'a>() -> &'a [u8] { #[cfg(not(target_os = "ziskos"))] pub fn read_input<'a>() -> &'a [u8] { - use std::fs::File; - use std::io::Read; - use std::mem; - use std::slice; + use std::{fs::File, io::Read, mem, slice}; let mut file = File::open("build/input.bin").unwrap(); let mut buffer = Vec::new(); From a9e3f6c228033f0981c4e1373ade7f4e7e99c442 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Thu, 24 Oct 2024 17:46:44 -0400 Subject: [PATCH 3/3] fix read_input and deps --- Cargo.lock | 1 - Cargo.toml | 2 ++ ziskos/entrypoint/Cargo.toml | 5 ++--- ziskos/entrypoint/src/lib.rs | 40 ++++++++++++++++++++++++++++-------- 4 files changed, 35 insertions(+), 13 deletions(-) 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 5a7e62be..192f198a 100644 --- a/ziskos/entrypoint/src/lib.rs +++ b/ziskos/entrypoint/src/lib.rs @@ -32,19 +32,41 @@ pub fn read_input<'a>() -> &'a [u8] { } #[cfg(not(target_os = "ziskos"))] -pub fn read_input<'a>() -> &'a [u8] { - use std::{fs::File, io::Read, mem, slice}; +use std::cell::RefCell; + +#[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); + } - let mut file = File::open("build/input.bin").unwrap(); - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer).unwrap(); + // 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)?; - let ptr = buffer.as_ptr(); - let len = buffer.len(); + // Leak the memory to get a 'static reference + let static_slice: &'static [u8] = Box::leak(buffer.into_boxed_slice()); - mem::forget(buffer); + // Memoize the result in thread-local storage + *file_content.borrow_mut() = Some(static_slice); - unsafe { slice::from_raw_parts(ptr, len) } + Ok(static_slice) + }) + .expect("Failed to read 'build/input.bin'") } #[cfg(target_os = "ziskos")]