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

perf: rm vec allocs from words conversions #1972

Merged
merged 2 commits into from
Jan 23, 2025
Merged
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
56 changes: 5 additions & 51 deletions crates/core/machine/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ use crate::memory::MemoryCols;
use generic_array::ArrayLength;
use p3_maybe_rayon::prelude::{ParallelBridge, ParallelIterator};

pub use sp1_primitives::consts::{
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<const N: usize>() -> [usize; N] {
let mut indices_arr = [0; N];
let mut i = 0;
Expand Down Expand Up @@ -97,57 +102,6 @@ pub fn next_power_of_two(n: usize, fixed_power: Option<usize>) -> usize {
}
}

/// Converts a slice of words to a slice of bytes in little endian.
pub fn words_to_bytes_le<const B: usize>(words: &[u32]) -> [u8; B] {
debug_assert_eq!(words.len() * 4, B);
words
.iter()
.flat_map(|word| word.to_le_bytes().to_vec())
.collect::<Vec<_>>()
.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<u8> {
words.iter().flat_map(|word| word.to_le_bytes().to_vec()).collect::<Vec<_>>()
}

/// Converts a byte array in little endian to a slice of words.
pub fn bytes_to_words_le<const W: usize>(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::<Vec<_>>()
.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<u32> {
bytes
.chunks_exact(4)
.map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap()))
.collect::<Vec<_>>()
}

/// Converts a num to a string with commas every 3 digits.
pub fn num_to_comma_separated<T: ToString>(value: T) -> String {
value
.to_string()
.chars()
.rev()
.collect::<Vec<_>>()
.chunks(3)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<_>>()
.join(",")
.chars()
.rev()
.collect()
}

pub fn chunk_vec<T>(mut vec: Vec<T>, chunk_size: usize) -> Vec<Vec<T>> {
let mut result = Vec::new();
while !vec.is_empty() {
Expand Down
18 changes: 5 additions & 13 deletions crates/primitives/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,21 @@ 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<u8> {
words.iter().flat_map(|word| word.to_le_bytes().to_vec()).collect::<Vec<_>>()
words.iter().flat_map(|word| word.to_le_bytes().into_iter()).collect::<Vec<_>>()
}

/// Converts a slice of words to a slice of bytes in little endian.
pub fn words_to_bytes_le<const B: usize>(words: &[u32]) -> [u8; B] {
debug_assert_eq!(words.len() * 4, B);
words
.iter()
.flat_map(|word| word.to_le_bytes().to_vec())
.collect::<Vec<_>>()
.try_into()
.unwrap()
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<const W: usize>(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::<Vec<_>>()
.try_into()
.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.
Expand Down
Loading