Skip to content

Commit

Permalink
[refactor] Switch from Rc<T> to Arc<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricedesre committed Nov 2, 2023
1 parent 6df4a6b commit ccdf22e
Show file tree
Hide file tree
Showing 26 changed files with 367 additions and 367 deletions.
12 changes: 6 additions & 6 deletions wnfs-bench/hamt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use criterion::{
Criterion, Throughput,
};
use proptest::{arbitrary::any, collection::vec, test_runner::TestRunner};
use std::{cmp, rc::Rc, sync::Arc};
use std::{cmp, sync::Arc};
use wnfs_common::{
async_encode, decode, libipld::cbor::DagCborCodec, utils::Sampleable, BlockStore, Link,
MemoryBlockStore,
Expand Down Expand Up @@ -33,7 +33,7 @@ fn node_set(c: &mut Criterion) {
(store, kv)
},
|(store, (key, value))| async move {
Rc::clone(node)
Arc::clone(node)
.set(key, value, store.as_ref())
.await
.unwrap();
Expand Down Expand Up @@ -75,7 +75,7 @@ fn node_set_consecutive(c: &mut Criterion) {
fn node_load_get(c: &mut Criterion) {
let store = MemoryBlockStore::default();
let cid = async_std::task::block_on(async {
let mut node = Rc::new(<Node<_, _>>::default());
let mut node = Arc::new(<Node<_, _>>::default());
for i in 0..50 {
node.set(i.to_string(), i, &store).await.unwrap();
}
Expand Down Expand Up @@ -107,7 +107,7 @@ fn node_load_get(c: &mut Criterion) {
fn node_load_remove(c: &mut Criterion) {
let store = MemoryBlockStore::default();
let cid = async_std::task::block_on(async {
let mut node = Rc::new(<Node<_, _>>::default());
let mut node = Arc::new(<Node<_, _>>::default());
for i in 0..50 {
node.set(i.to_string(), i, &store).await.unwrap();
}
Expand Down Expand Up @@ -136,7 +136,7 @@ fn node_load_remove(c: &mut Criterion) {
fn hamt_load_decode(c: &mut Criterion) {
let store = MemoryBlockStore::default();
let (cid, bytes) = async_std::task::block_on(async {
let mut node = Rc::new(<Node<_, _>>::default());
let mut node = Arc::new(<Node<_, _>>::default());
for i in 0..50 {
node.set(i.to_string(), i, &store).await.unwrap();
}
Expand Down Expand Up @@ -168,7 +168,7 @@ fn hamt_set_encode(c: &mut Criterion) {
|| {
(
MemoryBlockStore::default(),
Rc::new(<Node<_, _>>::default()),
Arc::new(<Node<_, _>>::default()),
)
},
|(store, mut node)| async move {
Expand Down
4 changes: 2 additions & 2 deletions wnfs-common/src/async_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::BlockStore;
use async_trait::async_trait;
use libipld::{error::SerdeError, serde as ipld_serde, Ipld};
use serde::{Serialize, Serializer};
use std::rc::Rc;
use std::sync::Arc;

//--------------------------------------------------------------------------------------------------
// Macros
Expand Down Expand Up @@ -60,7 +60,7 @@ pub trait AsyncSerialize {
//--------------------------------------------------------------------------------------------------

#[async_trait(?Send)]
impl<T: AsyncSerialize> AsyncSerialize for Rc<T> {
impl<T: AsyncSerialize> AsyncSerialize for Arc<T> {
async fn async_serialize<S, B>(&self, serializer: S, store: &B) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down
4 changes: 2 additions & 2 deletions wnfs-common/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use libipld::Cid;
use serde::de::DeserializeOwned;
use std::{
fmt::{self, Debug, Formatter},
rc::Rc,
sync::Arc,
};

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -256,7 +256,7 @@ pub trait RemembersCid {
fn persisted_as(&self) -> &OnceCell<Cid>;
}

impl<T: RemembersCid> RemembersCid for Rc<T> {
impl<T: RemembersCid> RemembersCid for Arc<T> {
fn persisted_as(&self) -> &OnceCell<Cid> {
self.as_ref().persisted_as()
}
Expand Down
6 changes: 3 additions & 3 deletions wnfs-common/src/pathnodes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::rc::Rc;
use std::sync::Arc;

//--------------------------------------------------------------------------------------------------
// Type Definitions
Expand All @@ -7,8 +7,8 @@ use std::rc::Rc;
/// Represents the directory nodes along a path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PathNodes<T> {
pub path: Vec<(Rc<T>, String)>,
pub tail: Rc<T>,
pub path: Vec<(Arc<T>, String)>,
pub tail: Arc<T>,
}

/// The kinds of outcome from getting a `PathNodes`.
Expand Down
74 changes: 37 additions & 37 deletions wnfs-hamt/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{Hasher, Node, Pair, Pointer, HAMT_BITMASK_BIT_SIZE};
use anyhow::{Ok, Result};
use async_recursion::async_recursion;
use serde::de::DeserializeOwned;
use std::{collections::HashMap, hash::Hash, mem, rc::Rc};
use std::{collections::HashMap, hash::Hash, mem, sync::Arc};
use wnfs_common::{BlockStore, Link};

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -41,30 +41,30 @@ pub struct KeyValueChange<K, V> {
/// # Examples
///
/// ```
/// use std::rc::Rc;
/// use std::sync::Arc;
/// use wnfs_hamt::{Node, Pair, diff};
/// use wnfs_common::{Link, MemoryBlockStore};
///
/// #[async_std::main]
/// async fn main() {
/// let store = &MemoryBlockStore::new();
/// let main_node = &mut Rc::new(Node::<[u8; 4], String>::default());
/// let main_node = &mut Arc::new(Node::<[u8; 4], String>::default());
/// for i in 0u32..3 {
/// main_node
/// .set(i.to_le_bytes(), i.to_string(), store)
/// .await
/// .unwrap();
/// }
///
/// let other_node = &mut Rc::new(Node::<[u8; 4], String>::default());
/// let other_node = &mut Arc::new(Node::<[u8; 4], String>::default());
/// other_node
/// .set(0_u32.to_le_bytes(), 0_u32.to_string(), store)
/// .await
/// .unwrap();
///
/// let changes = diff(
/// Link::from(Rc::clone(main_node)),
/// Link::from(Rc::clone(other_node)),
/// Link::from(Arc::clone(main_node)),
/// Link::from(Arc::clone(other_node)),
/// store,
/// )
/// .await
Expand All @@ -75,8 +75,8 @@ pub struct KeyValueChange<K, V> {
/// }
/// ```
pub async fn diff<K, V, H>(
main_link: Link<Rc<Node<K, V, H>>>,
other_link: Link<Rc<Node<K, V, H>>>,
main_link: Link<Arc<Node<K, V, H>>>,
other_link: Link<Arc<Node<K, V, H>>>,
store: &impl BlockStore,
) -> Result<Vec<KeyValueChange<K, V>>>
where
Expand All @@ -89,8 +89,8 @@ where

#[async_recursion(?Send)]
pub async fn diff_helper<K, V, H>(
main_link: Link<Rc<Node<K, V, H>>>,
other_link: Link<Rc<Node<K, V, H>>>,
main_link: Link<Arc<Node<K, V, H>>>,
other_link: Link<Arc<Node<K, V, H>>>,
depth: usize,
store: &impl BlockStore,
) -> Result<Vec<KeyValueChange<K, V>>>
Expand Down Expand Up @@ -140,15 +140,15 @@ where
// Main and other have a value. They may be the same or different so we check.
let main_index = main_node.get_value_index(index);
let main_pointer = mem::take(
Rc::make_mut(&mut main_node)
Arc::make_mut(&mut main_node)
.pointers
.get_mut(main_index)
.unwrap(),
);

let other_index = other_node.get_value_index(index);
let other_pointer = mem::take(
Rc::make_mut(&mut other_node)
Arc::make_mut(&mut other_node)
.pointers
.get_mut(other_index)
.unwrap(),
Expand Down Expand Up @@ -277,13 +277,13 @@ async fn create_node_from_pairs<K, V, H>(
values: Vec<Pair<K, V>>,
depth: usize,
store: &impl BlockStore,
) -> Result<Rc<Node<K, V, H>>>
) -> Result<Arc<Node<K, V, H>>>
where
K: DeserializeOwned + Clone + AsRef<[u8]>,
V: DeserializeOwned + Clone,
H: Hasher + Clone + 'static,
{
let mut node = Rc::new(Node::<_, _, H>::default());
let mut node = Arc::new(Node::<_, _, H>::default());
for Pair { key, value } in values {
let digest = &H::hash(&key);
let hashnibbles = &mut HashNibbles::with_cursor(digest, depth);
Expand All @@ -300,7 +300,7 @@ where
mod tests {
use super::{ChangeType::*, *};
use helper::*;
use std::{collections::BTreeSet, rc::Rc};
use std::collections::BTreeSet;
use wnfs_common::MemoryBlockStore;

mod helper {
Expand Down Expand Up @@ -335,23 +335,23 @@ mod tests {
async fn can_diff_main_node_with_added_removed_pairs() {
let store = &MemoryBlockStore::default();

let main_node = &mut Rc::new(Node::<[u8; 4], String>::default());
let main_node = &mut Arc::new(Node::<[u8; 4], String>::default());
for i in 0u32..3 {
main_node
.set(i.to_le_bytes(), i.to_string(), store)
.await
.unwrap();
}

let other_node = &mut Rc::new(Node::<[u8; 4], String>::default());
let other_node = &mut Arc::new(Node::<[u8; 4], String>::default());
other_node
.set(0_u32.to_le_bytes(), 0_u32.to_string(), store)
.await
.unwrap();

let changes = diff(
Link::from(Rc::clone(main_node)),
Link::from(Rc::clone(other_node)),
Link::from(Arc::clone(main_node)),
Link::from(Arc::clone(other_node)),
store,
)
.await
Expand All @@ -376,8 +376,8 @@ mod tests {
);

let changes = diff(
Link::from(Rc::clone(other_node)),
Link::from(Rc::clone(main_node)),
Link::from(Arc::clone(other_node)),
Link::from(Arc::clone(main_node)),
store,
)
.await
Expand Down Expand Up @@ -406,15 +406,15 @@ mod tests {
async fn can_diff_main_node_with_no_changes() {
let store = &MemoryBlockStore::default();

let main_node = &mut Rc::new(Node::<_, _>::default());
let main_node = &mut Arc::new(Node::<_, _>::default());
for i in 0_u32..3 {
main_node
.set(i.to_le_bytes(), i.to_string(), store)
.await
.unwrap();
}

let other_node = &mut Rc::new(Node::<_, _>::default());
let other_node = &mut Arc::new(Node::<_, _>::default());
for i in 0_u32..3 {
other_node
.set(i.to_le_bytes(), i.to_string(), store)
Expand All @@ -423,8 +423,8 @@ mod tests {
}

let changes = diff(
Link::from(Rc::clone(main_node)),
Link::from(Rc::clone(other_node)),
Link::from(Arc::clone(main_node)),
Link::from(Arc::clone(other_node)),
store,
)
.await
Expand All @@ -438,7 +438,7 @@ mod tests {
let store = &MemoryBlockStore::default();

// A node that adds the first 3 pairs of HASH_KV_PAIRS.
let other_node = &mut Rc::new(Node::<_, _, MockHasher>::default());
let other_node = &mut Arc::new(Node::<_, _, MockHasher>::default());
for (digest, kv) in HASH_KV_PAIRS.iter().take(3) {
other_node
.set_value(
Expand All @@ -452,7 +452,7 @@ mod tests {
}

// Another node that keeps the first pair, modify the second pair, removes the third pair, and adds the fourth and fifth pair.
let main_node = &mut Rc::new(Node::<_, _, MockHasher>::default());
let main_node = &mut Arc::new(Node::<_, _, MockHasher>::default());
main_node
.set_value(
&mut HashNibbles::new(&HASH_KV_PAIRS[0].0),
Expand Down Expand Up @@ -486,8 +486,8 @@ mod tests {
}

let changes = diff(
Link::from(Rc::clone(main_node)),
Link::from(Rc::clone(other_node)),
Link::from(Arc::clone(main_node)),
Link::from(Arc::clone(other_node)),
store,
)
.await
Expand Down Expand Up @@ -524,8 +524,8 @@ mod tests {
);

let changes = diff(
Link::from(Rc::clone(other_node)),
Link::from(Rc::clone(main_node)),
Link::from(Arc::clone(other_node)),
Link::from(Arc::clone(main_node)),
store,
)
.await
Expand Down Expand Up @@ -570,7 +570,7 @@ mod proptests {
ChangeType,
};
use async_std::task;
use std::{collections::HashSet, rc::Rc};
use std::{collections::HashSet, sync::Arc};
use test_strategy::proptest;
use wnfs_common::{Link, MemoryBlockStore};

Expand All @@ -590,14 +590,14 @@ mod proptests {
.await
.unwrap();

let main_node = &mut Rc::clone(other_node);
let main_node = &mut Arc::clone(other_node);
strategies::apply_changes(main_node, &strategy_changes, store)
.await
.unwrap();

let changes = super::diff(
Link::from(Rc::clone(main_node)),
Link::from(Rc::clone(other_node)),
Link::from(Arc::clone(main_node)),
Link::from(Arc::clone(other_node)),
store,
)
.await
Expand Down Expand Up @@ -654,8 +654,8 @@ mod proptests {
let node2 = strategies::node_from_kvs(kvs2, store).await.unwrap();

let changes = super::diff(
Link::from(Rc::clone(&node1)),
Link::from(Rc::clone(&node2)),
Link::from(Arc::clone(&node1)),
Link::from(Arc::clone(&node2)),
store,
)
.await
Expand Down
Loading

0 comments on commit ccdf22e

Please sign in to comment.