Skip to content

Commit

Permalink
Don't Box the children! (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle authored Mar 15, 2024
1 parent 513f097 commit 0d37ee3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ where
let children: [Option<Vec<u8>>; BranchNode::MAX_CHILDREN] = Default::default();
EncodedNode {
partial_path: n.partial_path.clone(),
children: Box::new(children),
children,
value: n.value.clone().into(),
phantom: PhantomData,
}
Expand Down Expand Up @@ -171,7 +171,7 @@ where
partial_path: encoded.partial_path,
children: [None; BranchNode::MAX_CHILDREN],
value: encoded.value,
children_encoded: *encoded.children,
children_encoded: encoded.children,
}
.into(),
))
Expand Down
22 changes: 14 additions & 8 deletions firewood/src/merkle/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,21 +463,27 @@ impl Storable for Node {
/// Contains the fields that we include in a node's hash.
/// If this is a leaf node, `children` is empty and `value` is Some.
/// If this is a branch node, `children` is non-empty.
#[derive(Debug)]
#[derive(Debug, Eq)]
pub struct EncodedNode<T> {
pub(crate) partial_path: Path,
/// If a child is None, it doesn't exist.
/// If it's Some, it's the value or value hash of the child.
pub(crate) children: Box<[Option<Vec<u8>>; BranchNode::MAX_CHILDREN]>,
pub(crate) children: [Option<Vec<u8>>; BranchNode::MAX_CHILDREN],
pub(crate) value: Option<Vec<u8>>,
pub(crate) phantom: PhantomData<T>,
}

// driving this adds an unnecessary bound, T: PartialEq
// PhantomData<T> is PartialEq for all T
impl<T> PartialEq for EncodedNode<T> {
fn eq(&self, other: &Self) -> bool {
self.partial_path == other.partial_path
&& self.children == other.children
&& self.value == other.value
let Self {
partial_path,
children,
value,
phantom: _,
} = self;
partial_path == &other.partial_path && children == &other.children && value == &other.value
}
}

Expand Down Expand Up @@ -536,7 +542,7 @@ impl<'de> Deserialize<'de> for EncodedNode<PlainCodec> {

Ok(Self {
partial_path: path,
children: children.into(),
children,
value,
phantom: PhantomData,
})
Expand Down Expand Up @@ -607,7 +613,7 @@ impl<'de> Deserialize<'de> for EncodedNode<Bincode> {
let children: [Option<Vec<u8>>; BranchNode::MAX_CHILDREN] = Default::default();
Ok(Self {
partial_path: path,
children: children.into(),
children,
value: Some(value),
phantom: PhantomData,
})
Expand All @@ -629,7 +635,7 @@ impl<'de> Deserialize<'de> for EncodedNode<Bincode> {

Ok(Self {
partial_path: path,
children: children.into(),
children,
value,
phantom: PhantomData,
})
Expand Down

0 comments on commit 0d37ee3

Please sign in to comment.