mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
Refactor tree hashing (#861)
* Pre-allocated tree hash caches * Add SmallVec to tree hash cache * Avoid allocation for validator.pubkey * Avoid iterator which seems to be doing heap alloc * Add more smallvecs * MOAR SMALLVEC * Move non-test code to Hash256 tree hash * Fix byte ordering error * Add incomplete but working merkle stream impl * Fix zero hash error * Add zero hash fn * Add MerkleStream comments * Add smallvec, tidy * Integrate into tree hash derive * Update ssz_types tree hash * Don't heap alloc for mix in length * Add byte-level streaming to MerkleStream * Avoid recursion in write method * Update BLS to MerkleStream * Fix some not-compiling tests * Remove debug profiling * Remove code duplication * Move beacon state tree hash to new hasher * Fix failing tests * Update comments * Add some fast-paths to tree_hash::merkle_root * Remove unncessary test * Rename MerkleStream -> MerkleHasher * Rename new_with_leaf_count -> with_leaves * Tidy * Remove NonZeroUsize * Remove todo * Update smallvec
This commit is contained in:
@@ -93,6 +93,7 @@ pub fn tree_hash_derive(input: TokenStream) -> TokenStream {
|
||||
};
|
||||
|
||||
let idents = get_hashable_fields(&struct_data);
|
||||
let num_leaves = idents.len();
|
||||
|
||||
let output = quote! {
|
||||
impl #impl_generics tree_hash::TreeHash for #name #ty_generics #where_clause {
|
||||
@@ -108,14 +109,15 @@ pub fn tree_hash_derive(input: TokenStream) -> TokenStream {
|
||||
unreachable!("Struct should never be packed.")
|
||||
}
|
||||
|
||||
fn tree_hash_root(&self) -> Vec<u8> {
|
||||
let mut leaves = Vec::with_capacity(4 * tree_hash::HASHSIZE);
|
||||
fn tree_hash_root(&self) -> tree_hash::Hash256 {
|
||||
let mut hasher = tree_hash::MerkleHasher::with_leaves(#num_leaves);
|
||||
|
||||
#(
|
||||
leaves.append(&mut self.#idents.tree_hash_root());
|
||||
hasher.write(self.#idents.tree_hash_root().as_bytes())
|
||||
.expect("tree hash derive should not apply too many leaves");
|
||||
)*
|
||||
|
||||
tree_hash::merkle_root(&leaves, 0)
|
||||
hasher.finish().expect("tree hash derive should not have a remaining buffer")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user