Skip to content

Commit

Permalink
Merge pull request #313 from Chia-Network/optimize-parse-vec
Browse files Browse the repository at this point in the history
optimize Vec<T>::parse(), for Streamable
  • Loading branch information
arvidn authored Nov 8, 2023
2 parents 1517f0f + a419156 commit b5b2653
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions chia-bls/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ fn to_lamport_pk(ikm: [u8; 32], idx: u32) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(lamport0);
hasher.update(lamport1);
hasher.finalize().try_into().unwrap()
hasher.finalize().into()
}

fn sha256(bytes: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(bytes);
hasher.finalize().try_into().unwrap()
hasher.finalize().into()
}

pub fn is_all_zero(buf: &[u8]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl<const N: usize> FromJsonDict for BytesImpl<N> {
N
)));
}
Ok((&buf).try_into()?)
Ok((&buf).into())
}
}

Expand Down
3 changes: 1 addition & 2 deletions chia-tools/src/bin/fast-forward-spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ fn main() {
let new_parents_parent: Bytes32 = hex::decode(args.new_parents_parent)
.expect("invalid hex")
.as_slice()
.try_into()
.expect("parent_id");
.into();

let mut a = Allocator::new_limited(500000000, 62500000, 62500000);
let puzzle = spend.puzzle_reveal.to_clvm(&mut a).expect("to_clvm");
Expand Down
31 changes: 28 additions & 3 deletions chia-traits/src/streamable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ impl<T: Streamable> Streamable for Vec<T> {
fn parse(input: &mut Cursor<&[u8]>) -> Result<Self> {
let len = u32::parse(input)?;

// TODO: pre-allocate capacity, but we'd need safe-guards for overflow
// attacks
let mut ret = Vec::<T>::new();
let mut ret = if std::mem::size_of::<T>() == 0 {
Vec::<T>::new()
} else {
let limit = 2 * 1024 * 1024 / std::mem::size_of::<T>();
Vec::<T>::with_capacity(std::cmp::min(limit, len as usize))
};
for _ in 0..len {
ret.push(T::parse(input)?);
}
Expand Down Expand Up @@ -172,6 +175,16 @@ impl Streamable for bool {
}
}

impl Streamable for () {
fn update_digest(&self, _digest: &mut Sha256) {}
fn stream(&self, _out: &mut Vec<u8>) -> Result<()> {
Ok(())
}
fn parse(_input: &mut Cursor<&[u8]>) -> Result<Self> {
Ok(())
}
}

impl<T: Streamable> Streamable for Option<T> {
fn update_digest(&self, digest: &mut Sha256) {
match self {
Expand Down Expand Up @@ -356,6 +369,12 @@ fn test_parse_list_list_3() {
from_bytes::<Vec<Vec<u32>>>(buf, vec![vec![0x01020304], vec![0x01030307], vec![0x402]]);
}

#[test]
fn test_parse_list_empty() {
let buf: &[u8] = &[0, 0, 0, 3];
from_bytes::<Vec<()>>(buf, vec![(), (), ()]);
}

#[test]
fn test_parse_long_list() {
let buf: &[u8] = &[0xff, 0xff, 0xff, 0xff, 0, 0, 0];
Expand Down Expand Up @@ -665,6 +684,12 @@ fn test_stream_list() {
);
}

#[test]
fn test_stream_list_of_empty() {
let out = stream(&vec![(), (), ()]);
assert_eq!(&out, &[0, 0, 0, 3]);
}

#[test]
fn test_stream_list_list() {
let out = stream(&vec![
Expand Down

0 comments on commit b5b2653

Please sign in to comment.