Reduce amount of clone() going on when applying data to MMRs (#2046)

* less cloning when pushing to PMMR

* pass a ref to data when applying to the MMR and appending to the backend

* cleanup

* rustfmt

* fixup tests
This commit is contained in:
Antioch Peverell
2018-12-02 19:59:24 +00:00
committed by GitHub
parent a3072efe70
commit 8bad93c8c9
11 changed files with 62 additions and 54 deletions
+1 -1
View File
@@ -162,7 +162,7 @@ impl Default for BlockHeader {
impl PMMRable for BlockHeader {
type E = Hash;
fn as_elmt(self) -> Self::E {
fn as_elmt(&self) -> Self::E {
self.hash()
}
}
+1 -1
View File
@@ -27,7 +27,7 @@ pub trait Backend<T: PMMRable> {
/// associated data element to flatfile storage (for leaf nodes only). The
/// position of the first element of the Vec in the MMR is provided to
/// help the implementation.
fn append(&mut self, data: T, hashes: Vec<Hash>) -> Result<(), String>;
fn append(&mut self, data: &T, hashes: Vec<Hash>) -> Result<(), String>;
/// Rewind the backend state to a previous position, as if all append
/// operations after that had been canceled. Expects a position in the PMMR
+1 -1
View File
@@ -171,7 +171,7 @@ where
/// Push a new element into the MMR. Computes new related peaks at
/// the same time if applicable.
pub fn push(&mut self, elmt: T) -> Result<u64, String> {
pub fn push(&mut self, elmt: &T) -> Result<u64, String> {
let elmt_pos = self.last_pos + 1;
let mut current_hash = elmt.hash_with_index(elmt_pos - 1);
+14 -4
View File
@@ -192,8 +192,8 @@ impl Readable for TxKernel {
impl PMMRable for TxKernel {
type E = TxKernelEntry;
fn as_elmt(self) -> Self::E {
self.into()
fn as_elmt(&self) -> TxKernelEntry {
TxKernelEntry::from_kernel(self)
}
}
@@ -260,6 +260,9 @@ impl TxKernel {
}
/// Wrapper around a tx kernel used when maintaining them in the MMR.
/// These will be useful once we implement relative lockheights via relative kernels
/// as a kernel may have an optional rel_kernel but we will not want to store these
/// directly in the kernel MMR.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TxKernelEntry {
/// The underlying tx kernel.
@@ -305,6 +308,13 @@ impl TxKernelEntry {
pub fn verify(&self) -> Result<(), Error> {
self.kernel.verify()
}
/// Build a new tx kernel entry from a kernel.
pub fn from_kernel(kernel: &TxKernel) -> TxKernelEntry {
TxKernelEntry {
kernel: kernel.clone(),
}
}
}
impl From<TxKernel> for TxKernelEntry {
@@ -1151,8 +1161,8 @@ impl Readable for Output {
impl PMMRable for Output {
type E = OutputIdentifier;
fn as_elmt(self) -> Self::E {
self.into()
fn as_elmt(&self) -> OutputIdentifier {
OutputIdentifier::from_output(self)
}
}
+3 -3
View File
@@ -515,8 +515,8 @@ impl FixedLength for RangeProof {
impl PMMRable for RangeProof {
type E = Self;
fn as_elmt(self) -> Self::E {
self
fn as_elmt(&self) -> Self::E {
self.clone()
}
}
@@ -695,7 +695,7 @@ pub trait PMMRable: Writeable + Clone + Debug {
type E: FixedLength + Readable + Writeable;
/// Convert the pmmrable into the element to be stored in the MMR data file.
fn as_elmt(self) -> Self::E;
fn as_elmt(&self) -> Self::E;
}
/// Generic trait to ensure PMMR elements can be hashed with an index