Skip to content

Commit

Permalink
Merge pull request #2320 from ljedrz/perf/assorted
Browse files Browse the repository at this point in the history
Assorted deserialization-related perf improvements
  • Loading branch information
howardwu authored Jan 28, 2024
2 parents 3f639c1 + fe137ba commit 8334cfb
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion console/algorithms/src/poseidon/hash_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ impl<E: Environment, const RATE: usize> HashMany for Poseidon<E, RATE> {

let mut sponge = PoseidonSponge::<E, RATE, CAPACITY>::new(&self.parameters);
sponge.absorb(&preimage);
sponge.squeeze(num_outputs).to_vec()
sponge.squeeze(num_outputs).into_vec()
}
}
3 changes: 2 additions & 1 deletion console/program/src/data/future/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl<N: Network> FromBytes for Future<N> {
// Read the argument (in 2 steps to prevent infinite recursion).
let num_bytes = u16::read_le(&mut reader)?;
// Read the argument bytes.
let bytes = (0..num_bytes).map(|_| u8::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
let mut bytes = Vec::new();
(&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
// Recover the argument.
let entry = Argument::read_le(&mut bytes.as_slice())?;
// Add the argument.
Expand Down
6 changes: 4 additions & 2 deletions console/program/src/data/plaintext/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl<N: Network> FromBytes for Plaintext<N> {
// Read the plaintext value (in 2 steps to prevent infinite recursion).
let num_bytes = u16::read_le(&mut reader)?;
// Read the plaintext bytes.
let bytes = (0..num_bytes).map(|_| u8::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
let mut bytes = Vec::new();
(&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
// Recover the plaintext value.
let plaintext = Plaintext::read_le(&mut bytes.as_slice())?;
// Add the member.
Expand All @@ -54,7 +55,8 @@ impl<N: Network> FromBytes for Plaintext<N> {
// Read the plaintext value (in 2 steps to prevent infinite recursion).
let num_bytes = u16::read_le(&mut reader)?;
// Read the plaintext bytes.
let bytes = (0..num_bytes).map(|_| u8::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
let mut bytes = Vec::new();
(&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
// Recover the plaintext value.
let plaintext = Plaintext::read_le(&mut bytes.as_slice())?;
// Add the element.
Expand Down
3 changes: 2 additions & 1 deletion console/program/src/data/record/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ impl<N: Network, Private: Visibility> FromBytes for Record<N, Private> {
// Read the entry value (in 2 steps to prevent infinite recursion).
let num_bytes = u16::read_le(&mut reader)?;
// Read the entry bytes.
let bytes = (0..num_bytes).map(|_| u8::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
let mut bytes = Vec::new();
(&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
// Recover the entry value.
let entry = Entry::read_le(&mut bytes.as_slice())?;
// Add the entry.
Expand Down
1 change: 1 addition & 0 deletions curves/src/templates/twisted_edwards_extended/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl<P: Parameters> AffineCurve for Affine<P> {
})
}

#[inline]
fn mul_bits(&self, bits: impl Iterator<Item = bool>) -> Projective<P> {
let mut res = Projective::zero();
for i in bits {
Expand Down
18 changes: 7 additions & 11 deletions fields/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ macro_rules! sqrt_impl {
let l_minus_one_times_k = n - 1 - k_2;
let l_minus_one = l_minus_one_times_k / k;
let l = l_minus_one + 1;
let mut l_s: Vec<u64> = Vec::with_capacity(k as usize);
l_s.resize(l_s.len() + k_1 as usize, l_minus_one);
l_s.resize(l_s.len() + k_2 as usize, l);

let l_s =
|| std::iter::repeat(l_minus_one).take(k_1 as usize).chain(std::iter::repeat(l).take(k_2 as usize));

let mut l_sum = 0;
let x_s = l_s.iter().take((k as usize) - 1).map(|l| {
let x_s = l_s().take((k as usize) - 1).map(|l| {
l_sum += l;
x.pow(BigInteger::from(2u64.pow((n - 1 - l_sum) as u32)))
});
Expand Down Expand Up @@ -140,20 +140,16 @@ macro_rules! sqrt_impl {
s
};

let calculate_kappa = |i: usize, j: usize, l_s: &[u64]| -> u64 {
l_s.iter().take(j).sum::<u64>() + 1 + l_s.iter().skip(i + 1).sum::<u64>()
};

let calculate_gamma = |i: usize, q_s: &[u64], last: bool| -> $Self {
let mut gamma = $Self::one();
if i != 0 {
q_s.iter().zip(l_s.iter()).enumerate().for_each(|(j, (q, l))| {
let mut kappa = calculate_kappa(i, j, &l_s);
q_s.iter().zip(l_s()).enumerate().for_each(|(j, (q, l))| {
let mut kappa = l_s().take(j).sum::<u64>() + 1 + l_s().skip(i + 1).sum::<u64>();
if last {
kappa -= 1;
}
let mut value = *q;
(0..*l as usize).for_each(|k| {
(0..l as usize).for_each(|k| {
let bit = value & 1 == 1;
if bit {
gamma *= $Self($P::POWERS_OF_ROOTS_OF_UNITY[(kappa as usize) + k], PhantomData);
Expand Down
9 changes: 5 additions & 4 deletions ledger/narwhal/data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ impl<T: FromBytes + ToBytes + Send + 'static> FromBytes for Data<T> {
return Err(error(format!("Failed to deserialize data ({num_bytes} bytes)")));
}
// Read the bytes.
let bytes = (0..num_bytes).map(|_| u8::read_le(&mut reader)).collect::<IoResult<Vec<u8>>>()?;
let mut bytes = Vec::new();
(&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
// Return the data.
Ok(Self::Buffer(Bytes::from(bytes)))
}
Expand All @@ -134,9 +135,9 @@ impl<T: FromBytes + ToBytes + Send + 'static> ToBytes for Data<T> {
// Write the data.
match self {
Self::Object(object) => {
// FIXME(ljedrz): see if we can omit this intermediate allocation.
let mut buffer = Vec::new();
object.write_le(&mut buffer)?;
// Serialize the object.
let buffer =
object.to_bytes_le().map_err(|err| error(format!("Failed to serialize a Data::Object: {err}")))?;
// Write the object.
u32::try_from(buffer.len()).map_err(error)?.write_le(&mut writer)?;
// Write the object.
Expand Down

0 comments on commit 8334cfb

Please sign in to comment.