Use smallvec for tree hash packed encoding

This commit is contained in:
Michael Sproul
2022-02-15 16:52:33 +11:00
parent e86cff2f8b
commit 5340c49de7
12 changed files with 31 additions and 73 deletions

View File

@@ -1,5 +1,6 @@
use super::*;
use ethereum_types::{H160, H256, U128, U256};
use smallvec::{smallvec, ToSmallVec};
fn int_to_hash256(int: u64) -> Hash256 {
let mut bytes = [0; HASHSIZE];
@@ -14,8 +15,8 @@ macro_rules! impl_for_bitsize {
TreeHashType::Basic
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
self.to_le_bytes().to_vec()
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.to_le_bytes().to_smallvec()
}
fn tree_hash_packing_factor() -> usize {
@@ -41,7 +42,7 @@ impl TreeHash for bool {
TreeHashType::Basic
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
(*self as u8).tree_hash_packed_encoding()
}
@@ -62,7 +63,7 @@ macro_rules! impl_for_lt_32byte_u8_array {
TreeHashType::Vector
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
unreachable!("bytesN should never be packed.")
}
@@ -87,8 +88,8 @@ impl TreeHash for U128 {
TreeHashType::Basic
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 16];
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
let mut result = smallvec![0; 16];
self.to_little_endian(&mut result);
result
}
@@ -109,8 +110,8 @@ impl TreeHash for U256 {
TreeHashType::Basic
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 32];
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
let mut result = smallvec![0; 32];
self.to_little_endian(&mut result);
result
}
@@ -131,8 +132,8 @@ impl TreeHash for H160 {
TreeHashType::Vector
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 32];
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
let mut result = smallvec![0; 32];
result[0..20].copy_from_slice(self.as_bytes());
result
}
@@ -153,8 +154,8 @@ impl TreeHash for H256 {
TreeHashType::Vector
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
self.as_bytes().to_vec()
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.as_bytes().to_smallvec()
}
fn tree_hash_packing_factor() -> usize {