Add first pass of grow cache algo

This commit is contained in:
Paul Hauner 2019-04-13 09:42:43 +10:00
parent a124042e30
commit 75177837d0
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
3 changed files with 34 additions and 17 deletions

View File

@ -6,6 +6,7 @@ use std::ops::Range;
use std::vec::Splice;
mod impls;
mod resize;
mod tests;
const BYTES_PER_CHUNK: usize = 32;

View File

@ -170,23 +170,6 @@ where
}
}
/// New vec is bigger than old vec.
fn grow_merkle_cache(cache: Vec<u8>, to: usize) -> Vec<u8> {
let new = Vec::with_capacity(to * HASHSIZE);
let i = cache.len() / HASHSIZE;
let j = to;
assert_eq!(i.next_power_of_two(), i);
assert_eq!(j.next_power_of_two(), j);
while i > 0 {
}
new
}
fn get_packed_leaves<T>(vec: &Vec<T>) -> Vec<u8>
where
T: CachedTreeHash<T>,

View File

@ -0,0 +1,33 @@
use super::*;
/// New vec is bigger than old vec.
fn grow_merkle_cache(old_bytes: &[u8], old_flags: &[bool], to: usize) -> Option<Vec<u8>> {
let mut bytes = Vec::with_capacity(to * HASHSIZE);
let mut flags = Vec::with_capacity(to);
let from = old_bytes.len() / HASHSIZE;
let to = to;
let distance = (from.leading_zeros() - to.leading_zeros()) as usize;
let leading_zero_chunks = 1 >> distance;
bytes.resize(leading_zero_chunks * HASHSIZE, 0);
flags.resize(leading_zero_chunks, true); // all new chunks are modified by default.
for i in 0..to.leading_zeros() as usize {
let new_slice = bytes.get_mut(1 >> i + distance..1 >> i + distance + 1)?;
let old_slice = old_bytes.get(1 >> i..1 >> i + 1)?;
new_slice.copy_from_slice(old_slice);
}
Some(bytes)
}
#[cfg(test)]
mod test {
#[test]
fn can_grow() {
// TODO
}
}