mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
## Issue Addressed NA ## Proposed Changes I've noticed that our block hashing times increase significantly after the merge. I did some flamegraph-ing and noticed that we're allocating a `Vec` for each byte of each execution payload transaction. This seems like unnecessary work and a bit of a fragmentation risk. This PR switches to `SmallVec<[u8; 32]>` for the packed encoding of `TreeHash`. I believe this is a nice simple optimisation with no downside. ### Benchmarking These numbers were computed using #3580 on my desktop (i7 hex-core). You can see a bit of noise in the numbers, that's probably just my computer doing other things. Generally I found this change takes the time from 10-11ms to 8-9ms. I can also see all the allocations disappear from flamegraph. This is the block being benchmarked: https://beaconcha.in/slot/4704236 #### Before ``` [2022-09-15T21:44:19Z INFO lcli::block_root] Run 980: 10.553003ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 981: 10.563737ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 982: 10.646352ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 983: 10.628532ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 984: 10.552112ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 985: 10.587778ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 986: 10.640526ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 987: 10.587243ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 988: 10.554748ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 989: 10.551111ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 990: 11.559031ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 991: 11.944827ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 992: 10.554308ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 993: 11.043397ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 994: 11.043315ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 995: 11.207711ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 996: 11.056246ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 997: 11.049706ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 998: 11.432449ms [2022-09-15T21:44:19Z INFO lcli::block_root] Run 999: 11.149617ms ``` #### After ``` [2022-09-15T21:41:49Z INFO lcli::block_root] Run 980: 14.011653ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 981: 8.925314ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 982: 8.849563ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 983: 8.893689ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 984: 8.902964ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 985: 8.942067ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 986: 8.907088ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 987: 9.346101ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 988: 8.96142ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 989: 9.366437ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 990: 9.809334ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 991: 9.541561ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 992: 11.143518ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 993: 10.821181ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 994: 9.855973ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 995: 10.941006ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 996: 9.596155ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 997: 9.121739ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 998: 9.090019ms [2022-09-15T21:41:49Z INFO lcli::block_root] Run 999: 9.071885ms ``` ## Additional Info Please provide any additional information. For example, future considerations or information useful for reviewers.
203 lines
5.1 KiB
Rust
203 lines
5.1 KiB
Rust
use super::*;
|
|
use ethereum_types::{H160, H256, U128, U256};
|
|
|
|
fn int_to_hash256(int: u64) -> Hash256 {
|
|
let mut bytes = [0; HASHSIZE];
|
|
bytes[0..8].copy_from_slice(&int.to_le_bytes());
|
|
Hash256::from_slice(&bytes)
|
|
}
|
|
|
|
macro_rules! impl_for_bitsize {
|
|
($type: ident, $bit_size: expr) => {
|
|
impl TreeHash for $type {
|
|
fn tree_hash_type() -> TreeHashType {
|
|
TreeHashType::Basic
|
|
}
|
|
|
|
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
|
PackedEncoding::from_slice(&self.to_le_bytes())
|
|
}
|
|
|
|
fn tree_hash_packing_factor() -> usize {
|
|
HASHSIZE / ($bit_size / 8)
|
|
}
|
|
|
|
#[allow(clippy::cast_lossless)] // Lint does not apply to all uses of this macro.
|
|
fn tree_hash_root(&self) -> Hash256 {
|
|
int_to_hash256(*self as u64)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl_for_bitsize!(u8, 8);
|
|
impl_for_bitsize!(u16, 16);
|
|
impl_for_bitsize!(u32, 32);
|
|
impl_for_bitsize!(u64, 64);
|
|
impl_for_bitsize!(usize, 64);
|
|
|
|
impl TreeHash for bool {
|
|
fn tree_hash_type() -> TreeHashType {
|
|
TreeHashType::Basic
|
|
}
|
|
|
|
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
|
(*self as u8).tree_hash_packed_encoding()
|
|
}
|
|
|
|
fn tree_hash_packing_factor() -> usize {
|
|
u8::tree_hash_packing_factor()
|
|
}
|
|
|
|
fn tree_hash_root(&self) -> Hash256 {
|
|
int_to_hash256(*self as u64)
|
|
}
|
|
}
|
|
|
|
/// Only valid for byte types less than 32 bytes.
|
|
macro_rules! impl_for_lt_32byte_u8_array {
|
|
($len: expr) => {
|
|
impl TreeHash for [u8; $len] {
|
|
fn tree_hash_type() -> TreeHashType {
|
|
TreeHashType::Vector
|
|
}
|
|
|
|
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
|
unreachable!("bytesN should never be packed.")
|
|
}
|
|
|
|
fn tree_hash_packing_factor() -> usize {
|
|
unreachable!("bytesN should never be packed.")
|
|
}
|
|
|
|
fn tree_hash_root(&self) -> Hash256 {
|
|
let mut result = [0; 32];
|
|
result[0..$len].copy_from_slice(&self[..]);
|
|
Hash256::from_slice(&result)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
impl_for_lt_32byte_u8_array!(4);
|
|
impl_for_lt_32byte_u8_array!(32);
|
|
|
|
impl TreeHash for U128 {
|
|
fn tree_hash_type() -> TreeHashType {
|
|
TreeHashType::Basic
|
|
}
|
|
|
|
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
|
let mut result = [0; 16];
|
|
self.to_little_endian(&mut result);
|
|
PackedEncoding::from_slice(&result)
|
|
}
|
|
|
|
fn tree_hash_packing_factor() -> usize {
|
|
2
|
|
}
|
|
|
|
fn tree_hash_root(&self) -> Hash256 {
|
|
let mut result = [0; HASHSIZE];
|
|
self.to_little_endian(&mut result[0..16]);
|
|
Hash256::from_slice(&result)
|
|
}
|
|
}
|
|
|
|
impl TreeHash for U256 {
|
|
fn tree_hash_type() -> TreeHashType {
|
|
TreeHashType::Basic
|
|
}
|
|
|
|
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
|
let mut result = [0; 32];
|
|
self.to_little_endian(&mut result);
|
|
PackedEncoding::from_slice(&result)
|
|
}
|
|
|
|
fn tree_hash_packing_factor() -> usize {
|
|
1
|
|
}
|
|
|
|
fn tree_hash_root(&self) -> Hash256 {
|
|
let mut result = [0; 32];
|
|
self.to_little_endian(&mut result[..]);
|
|
Hash256::from_slice(&result)
|
|
}
|
|
}
|
|
|
|
impl TreeHash for H160 {
|
|
fn tree_hash_type() -> TreeHashType {
|
|
TreeHashType::Vector
|
|
}
|
|
|
|
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
|
let mut result = [0; 32];
|
|
result[0..20].copy_from_slice(self.as_bytes());
|
|
PackedEncoding::from_slice(&result)
|
|
}
|
|
|
|
fn tree_hash_packing_factor() -> usize {
|
|
1
|
|
}
|
|
|
|
fn tree_hash_root(&self) -> Hash256 {
|
|
let mut result = [0; 32];
|
|
result[0..20].copy_from_slice(self.as_bytes());
|
|
Hash256::from_slice(&result)
|
|
}
|
|
}
|
|
|
|
impl TreeHash for H256 {
|
|
fn tree_hash_type() -> TreeHashType {
|
|
TreeHashType::Vector
|
|
}
|
|
|
|
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
|
|
PackedEncoding::from_slice(self.as_bytes())
|
|
}
|
|
|
|
fn tree_hash_packing_factor() -> usize {
|
|
1
|
|
}
|
|
|
|
fn tree_hash_root(&self) -> Hash256 {
|
|
*self
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn bool() {
|
|
let mut true_bytes: Vec<u8> = vec![1];
|
|
true_bytes.append(&mut vec![0; 31]);
|
|
|
|
let false_bytes: Vec<u8> = vec![0; 32];
|
|
|
|
assert_eq!(true.tree_hash_root().as_bytes(), true_bytes.as_slice());
|
|
assert_eq!(false.tree_hash_root().as_bytes(), false_bytes.as_slice());
|
|
}
|
|
|
|
#[test]
|
|
fn int_to_bytes() {
|
|
assert_eq!(int_to_hash256(0).as_bytes(), &[0; 32]);
|
|
assert_eq!(
|
|
int_to_hash256(1).as_bytes(),
|
|
&[
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0
|
|
]
|
|
);
|
|
assert_eq!(
|
|
int_to_hash256(u64::max_value()).as_bytes(),
|
|
&[
|
|
255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
]
|
|
);
|
|
}
|
|
}
|