From 7e380d6059741f67af43c316d7fd2ebc096da9bb Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 23 Jan 2025 01:21:28 +0100 Subject: [PATCH 1/2] perf: rm vec allocs from words conversions --- crates/core/machine/src/utils/mod.rs | 55 ++-------------------------- crates/primitives/src/consts.rs | 18 ++++----- 2 files changed, 11 insertions(+), 62 deletions(-) diff --git a/crates/core/machine/src/utils/mod.rs b/crates/core/machine/src/utils/mod.rs index 7775c2afbd..dcc9b99372 100644 --- a/crates/core/machine/src/utils/mod.rs +++ b/crates/core/machine/src/utils/mod.rs @@ -18,6 +18,10 @@ use crate::memory::MemoryCols; use generic_array::ArrayLength; use p3_maybe_rayon::prelude::{ParallelBridge, ParallelIterator}; +pub use sp1_primitives::consts::{ + words_to_bytes_le, words_to_bytes_le_vec, bytes_to_words_le, bytes_to_words_le_vec, num_to_comma_separated +}; + pub const fn indices_arr() -> [usize; N] { let mut indices_arr = [0; N]; let mut i = 0; @@ -97,57 +101,6 @@ pub fn next_power_of_two(n: usize, fixed_power: Option) -> usize { } } -/// Converts a slice of words to a slice of bytes in little endian. -pub fn words_to_bytes_le(words: &[u32]) -> [u8; B] { - debug_assert_eq!(words.len() * 4, B); - words - .iter() - .flat_map(|word| word.to_le_bytes().to_vec()) - .collect::>() - .try_into() - .unwrap() -} - -/// Converts a slice of words to a byte vector in little endian. -pub fn words_to_bytes_le_vec(words: &[u32]) -> Vec { - words.iter().flat_map(|word| word.to_le_bytes().to_vec()).collect::>() -} - -/// Converts a byte array in little endian to a slice of words. -pub fn bytes_to_words_le(bytes: &[u8]) -> [u32; W] { - debug_assert_eq!(bytes.len(), W * 4); - bytes - .chunks_exact(4) - .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())) - .collect::>() - .try_into() - .unwrap() -} - -/// Converts a byte array in little endian to a vector of words. -pub fn bytes_to_words_le_vec(bytes: &[u8]) -> Vec { - bytes - .chunks_exact(4) - .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())) - .collect::>() -} - -/// Converts a num to a string with commas every 3 digits. -pub fn num_to_comma_separated(value: T) -> String { - value - .to_string() - .chars() - .rev() - .collect::>() - .chunks(3) - .map(|chunk| chunk.iter().collect::()) - .collect::>() - .join(",") - .chars() - .rev() - .collect() -} - pub fn chunk_vec(mut vec: Vec, chunk_size: usize) -> Vec> { let mut result = Vec::new(); while !vec.is_empty() { diff --git a/crates/primitives/src/consts.rs b/crates/primitives/src/consts.rs index 52c72afc3e..4f82ac1ba1 100644 --- a/crates/primitives/src/consts.rs +++ b/crates/primitives/src/consts.rs @@ -60,29 +60,25 @@ pub mod fd { /// Converts a slice of words to a byte vector in little endian. pub fn words_to_bytes_le_vec(words: &[u32]) -> Vec { - words.iter().flat_map(|word| word.to_le_bytes().to_vec()).collect::>() + words.iter().flat_map(|word| word.to_le_bytes().into_iter()).collect::>() } /// Converts a slice of words to a slice of bytes in little endian. pub fn words_to_bytes_le(words: &[u32]) -> [u8; B] { debug_assert_eq!(words.len() * 4, B); - words + let mut iter = words .iter() - .flat_map(|word| word.to_le_bytes().to_vec()) - .collect::>() - .try_into() - .unwrap() + .flat_map(|word| word.to_le_bytes().into_iter()); + core::array::from_fn(|_| iter.next().unwrap()) } /// Converts a byte array in little endian to a slice of words. pub fn bytes_to_words_le(bytes: &[u8]) -> [u32; W] { debug_assert_eq!(bytes.len(), W * 4); - bytes + let mut iter = bytes .chunks_exact(4) - .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())) - .collect::>() - .try_into() - .unwrap() + .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())); + core::array::from_fn(|_| iter.next().unwrap()) } /// Converts a byte array in little endian to a vector of words. From 5ff206022472b4a5fe00b76b89494edcb86ae2b6 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 23 Jan 2025 01:38:07 +0100 Subject: [PATCH 2/2] rustfmt --- crates/core/machine/src/utils/mod.rs | 3 ++- crates/primitives/src/consts.rs | 10 +++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/core/machine/src/utils/mod.rs b/crates/core/machine/src/utils/mod.rs index dcc9b99372..aba6edcb21 100644 --- a/crates/core/machine/src/utils/mod.rs +++ b/crates/core/machine/src/utils/mod.rs @@ -19,7 +19,8 @@ use generic_array::ArrayLength; use p3_maybe_rayon::prelude::{ParallelBridge, ParallelIterator}; pub use sp1_primitives::consts::{ - words_to_bytes_le, words_to_bytes_le_vec, bytes_to_words_le, bytes_to_words_le_vec, num_to_comma_separated + bytes_to_words_le, bytes_to_words_le_vec, num_to_comma_separated, words_to_bytes_le, + words_to_bytes_le_vec, }; pub const fn indices_arr() -> [usize; N] { diff --git a/crates/primitives/src/consts.rs b/crates/primitives/src/consts.rs index 4f82ac1ba1..b179624f22 100644 --- a/crates/primitives/src/consts.rs +++ b/crates/primitives/src/consts.rs @@ -66,19 +66,15 @@ pub fn words_to_bytes_le_vec(words: &[u32]) -> Vec { /// Converts a slice of words to a slice of bytes in little endian. pub fn words_to_bytes_le(words: &[u32]) -> [u8; B] { debug_assert_eq!(words.len() * 4, B); - let mut iter = words - .iter() - .flat_map(|word| word.to_le_bytes().into_iter()); + let mut iter = words.iter().flat_map(|word| word.to_le_bytes().into_iter()); core::array::from_fn(|_| iter.next().unwrap()) } /// Converts a byte array in little endian to a slice of words. pub fn bytes_to_words_le(bytes: &[u8]) -> [u32; W] { debug_assert_eq!(bytes.len(), W * 4); - let mut iter = bytes - .chunks_exact(4) - .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())); - core::array::from_fn(|_| iter.next().unwrap()) + let mut iter = bytes.chunks_exact(4).map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())); + core::array::from_fn(|_| iter.next().unwrap()) } /// Converts a byte array in little endian to a vector of words.