Skip to content

Commit

Permalink
Use a boxed slice instead of a vec (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuris authored Sep 6, 2024
1 parent 4bbb4da commit 56decb8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions firewood/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ enum PathIteratorState<'a> {
}

/// Iterates over all nodes on the path to a given key starting from the root.
///
/// All nodes are branch nodes except possibly the last, which may be a leaf.
/// All returned nodes have keys which are a prefix of the given key.
/// If the given key is in the trie, the last node is at that key.
Expand Down Expand Up @@ -1262,9 +1263,8 @@ mod tests {
let mut merkle = create_test_merkle();

let keys: Vec<_> = children
.map(|key| {
merkle.insert(&key, key.clone().into()).unwrap();
key
.inspect(|key| {
merkle.insert(key, key.clone().into()).unwrap();
})
.collect();

Expand Down
15 changes: 9 additions & 6 deletions firewood/src/v2/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<T: api::DbView> Clone for ProposalBase<T> {
#[derive(Debug)]
pub struct Proposal<T> {
pub(crate) base: ProposalBase<T>,
pub(crate) delta: BTreeMap<Vec<u8>, KeyOp<Vec<u8>>>,
pub(crate) delta: BTreeMap<Box<[u8]>, KeyOp<Box<[u8]>>>,
}

// Implement Clone because T doesn't need to be Clone
Expand All @@ -90,12 +90,15 @@ impl<T> Proposal<T> {
let delta = batch
.into_iter()
.map(|op| match op {
api::BatchOp::Put { key, value } => {
(key.as_ref().to_vec(), KeyOp::Put(value.as_ref().to_vec()))
api::BatchOp::Put { key, value } => (
key.as_ref().to_vec().into_boxed_slice(),
KeyOp::Put(value.as_ref().to_vec().into_boxed_slice()),
),
api::BatchOp::Delete { key } => {
(key.as_ref().to_vec().into_boxed_slice(), KeyOp::Delete)
}
api::BatchOp::Delete { key } => (key.as_ref().to_vec(), KeyOp::Delete),
})
.collect();
.collect::<BTreeMap<_, _>>();

Arc::new(Self { base, delta })
}
Expand All @@ -115,7 +118,7 @@ impl<T: api::DbView + Send + Sync> api::DbView for Proposal<T> {
match self.delta.get(key.as_ref()) {
Some(change) => match change {
// key in proposal, check for Put or Delete
KeyOp::Put(val) => Ok(Some(val.clone().into_boxed_slice())),
KeyOp::Put(val) => Ok(Some(val.clone())),
KeyOp::Delete => Ok(None), // key was deleted in this proposal
},
None => match &self.base {
Expand Down
2 changes: 2 additions & 0 deletions storage/src/nodestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
}

/// Some nodestore kinds implement Parentable.
///
/// This means that the nodestore can have children.
/// Only [ImmutableProposal] and [Committed] implement this trait.
/// [MutableProposal] does not implement this trait because it is not a valid parent.
Expand Down Expand Up @@ -709,6 +710,7 @@ where
}

/// Contains the state of a revision of a merkle trie.
///
/// The first generic parameter is the type of the revision, which supports reading nodes from parent proposals.
/// The second generic parameter is the type of the storage used, either
/// in-memory or on-disk.
Expand Down

0 comments on commit 56decb8

Please sign in to comment.