Skip to content

Commit

Permalink
use extend_from_slice when appending multiple hashes to hash file (#3497
Browse files Browse the repository at this point in the history
)

* use extend_from_slice when appending multiple hashes to hash file

* bump
  • Loading branch information
antiochp authored Nov 24, 2020
1 parent 055b684 commit 2125c05
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/src/core/pmmr/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &[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
Expand Down
2 changes: 1 addition & 1 deletion core/src/core/pmmr/pmmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ where
}

// append all the new nodes and update the MMR index
self.backend.append(elmt, hashes)?;
self.backend.append(elmt, &hashes)?;
self.last_pos = pos;
Ok(elmt_pos)
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/core/pmmr/vec_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pub struct VecBackend<T: PMMRable> {
}

impl<T: PMMRable> Backend<T> for VecBackend<T> {
fn append(&mut self, elmt: &T, hashes: Vec<Hash>) -> Result<(), String> {
fn append(&mut self, elmt: &T, hashes: &[Hash]) -> Result<(), String> {
if let Some(data) = &mut self.data {
data.push(elmt.clone());
}
self.hashes.append(&mut hashes.clone());
self.hashes.extend_from_slice(hashes);
Ok(())
}

Expand Down
10 changes: 4 additions & 6 deletions store/src/pmmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@ impl<T: PMMRable> Backend<T> for PMMRBackend<T> {
/// Append the provided data and hashes to the backend storage.
/// Add the new leaf pos to our leaf_set if this is a prunable MMR.
#[allow(unused_variables)]
fn append(&mut self, data: &T, hashes: Vec<Hash>) -> Result<(), String> {
fn append(&mut self, data: &T, hashes: &[Hash]) -> Result<(), String> {
let size = self
.data_file
.append(&data.as_elmt())
.map_err(|e| format!("Failed to append data to file. {}", e))?;

for h in &hashes {
self.hash_file
.append(h)
.map_err(|e| format!("Failed to append hash to file. {}", e))?;
}
self.hash_file
.extend_from_slice(hashes)
.map_err(|e| format!("Failed to append hash to file. {}", e))?;

if self.prunable {
// (Re)calculate the latest pos given updated size of data file
Expand Down
16 changes: 16 additions & 0 deletions store/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ where
Ok(self.size_unsync())
}

/// Append a slice of multiple elements to the file.
/// Will not be written to disk until flush() is subsequently called.
/// Alternatively discard() may be called to discard any pending changes.
pub fn extend_from_slice(&mut self, data: &[T]) -> io::Result<u64> {
self.file.append_elmts(data)?;
Ok(self.size_unsync())
}

/// Read an element from the file by position.
/// Assumes we have already "shifted" the position to account for pruned data.
/// Note: PMMR API is 1-indexed, but backend storage is 0-indexed.
Expand Down Expand Up @@ -280,6 +288,14 @@ where
Ok(())
}

/// Iterate over the slice and append each element.
fn append_elmts(&mut self, data: &[T]) -> io::Result<()> {
for x in data {
self.append_elmt(x)?;
}
Ok(())
}

/// Append data to the file. Until the append-only file is synced, data is
/// only written to memory.
pub fn append(&mut self, bytes: &mut [u8]) -> io::Result<()> {
Expand Down

0 comments on commit 2125c05

Please sign in to comment.