Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a boxed slice instead of a vec #718

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading