mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-17 21:08:32 +00:00
Lazy hashing for SignedBeaconBlock in sync (#2916)
## Proposed Changes Allocate less memory in sync by hashing the `SignedBeaconBlock`s in a batch directly, rather than going via SSZ bytes. Credit to @paulhauner for finding this source of temporary allocations.
This commit is contained in:
@@ -9,6 +9,7 @@ use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use ssz::{Decode, Encode};
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::marker::PhantomData;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
@@ -264,6 +265,18 @@ where
|
||||
impl_tree_hash!(SIGNATURE_BYTES_LEN);
|
||||
}
|
||||
|
||||
/// Hashes the `self.serialize()` bytes.
|
||||
#[allow(clippy::derive_hash_xor_eq)]
|
||||
impl<Pub, AggPub, Sig, AggSig> Hash for GenericAggregateSignature<Pub, AggPub, Sig, AggSig>
|
||||
where
|
||||
Sig: TSignature<Pub>,
|
||||
AggSig: TAggregateSignature<Pub, AggPub, Sig>,
|
||||
{
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.serialize().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<Pub, AggPub, Sig, AggSig> fmt::Display for GenericAggregateSignature<Pub, AggPub, Sig, AggSig>
|
||||
where
|
||||
Sig: TSignature<Pub>,
|
||||
|
||||
@@ -7,6 +7,7 @@ use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use ssz::{Decode, Encode};
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::marker::PhantomData;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
@@ -145,6 +146,13 @@ impl<PublicKey, T: TSignature<PublicKey>> TreeHash for GenericSignature<PublicKe
|
||||
impl_tree_hash!(SIGNATURE_BYTES_LEN);
|
||||
}
|
||||
|
||||
/// Hashes the `self.serialize()` bytes.
|
||||
impl<PublicKey, T: TSignature<PublicKey>> Hash for GenericSignature<PublicKey, T> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.serialize().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<PublicKey, T: TSignature<PublicKey>> fmt::Display for GenericSignature<PublicKey, T> {
|
||||
impl_display!();
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use serde::ser::{Serialize, Serializer};
|
||||
use ssz::{Decode, Encode};
|
||||
use std::convert::TryInto;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::marker::PhantomData;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
@@ -84,6 +85,12 @@ impl<Pub, Sig> PartialEq for GenericSignatureBytes<Pub, Sig> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Pub, Sig> Hash for GenericSignatureBytes<Pub, Sig> {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
self.bytes.hash(hasher);
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializes the `GenericSignature` in compressed form, storing the bytes in the newly created `Self`.
|
||||
impl<Pub, Sig> From<GenericSignature<Pub, Sig>> for GenericSignatureBytes<Pub, Sig>
|
||||
where
|
||||
|
||||
@@ -113,6 +113,14 @@ impl PartialEq for Signature {
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Signature {}
|
||||
|
||||
impl std::hash::Hash for Signature {
|
||||
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
|
||||
self.0.hash(hasher);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AggregateSignature([u8; SIGNATURE_BYTES_LEN]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user