Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor read_input function to return a borrowed slice instead of a … #126

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
5 changes: 2 additions & 3 deletions ziskos/entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 36 additions & 10 deletions ziskos/entrypoint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,51 @@
use crate::ziskos_definitions::ziskos_config::*;

#[cfg(target_os = "ziskos")]
pub fn read_input() -> Vec<u8> {
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<u8> {
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<Option<&'static [u8]>> = RefCell::new(None);

Check failure on line 40 in ziskos/entrypoint/src/lib.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

initializer for `thread_local` value can be made `const`
}

#[cfg(not(target_os = "ziskos"))]
pub fn read_input<'a>() -> &'static [u8] {

Check failure on line 44 in ziskos/entrypoint/src/lib.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

this lifetime isn't used in the function definition
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<u8>
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")]
Expand Down
Loading