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

Add caching to Hamt and update testing #730

Merged
merged 8 commits into from
Oct 6, 2020
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
11 changes: 2 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions blockchain/state_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ where
) -> Result<(power::Claim, power::Claim), Error> {
let ps: power::State = self.load_actor_state(&*STORAGE_POWER_ACTOR_ADDR, state_cid)?;

let cm = make_map_with_root(&ps.claims, self.bs.as_ref())
let cm = make_map_with_root::<_, power::Claim>(&ps.claims, self.bs.as_ref())
.map_err(|e| Error::State(e.to_string()))?;
let claim: power::Claim = cm
let claim = cm
.get(&addr.to_bytes())
.map_err(|e| Error::State(e.to_string()))?
.ok_or_else(|| {
Error::State("Failed to retrieve claimed power from actor state".to_owned())
})?;
})?
.clone();
Ok((
claim,
power::Claim {
Expand Down Expand Up @@ -800,12 +801,12 @@ where
escrow: {
let et = BalanceTable::from_root(self.bs.as_ref(), &market_state.escrow_table)
.map_err(|_x| Error::State("Failed to build Escrow Table".to_string()))?;
et.get(&new_addr).unwrap_or_default()
et.get(&new_addr).map(Clone::clone).unwrap_or_default()
},
locked: {
let lt = BalanceTable::from_root(self.bs.as_ref(), &market_state.locked_table)
.map_err(|_x| Error::State("Failed to build Locked Table".to_string()))?;
lt.get(&new_addr).unwrap_or_default()
lt.get(&new_addr).map(Clone::clone).unwrap_or_default()
},
};

Expand Down
12 changes: 4 additions & 8 deletions ipld/hamt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ forest_ipld = { path = "../" }
serde_bytes = "0.11.3"
thiserror = "1.0"
sha2 = "0.9.1"

# TODO remove murmur3 support when finalized in Filecoin
[dependencies.murmur3]
git = "https://github.com/dignifiedquire/murmur3"
branch = "nicer-hashing"
optional = true
lazycell = "1.2.1"

[features]
default = ["identity", "murmur3"]
default = ["identity"]
identity = []
murmur = ["murmur3"]

[dev-dependencies]
hex = "0.4.2"
criterion = "0.3.3"
ipld_blockstore = { path = "../blockstore", features = ["tracking"] }
unsigned-varint = "0.5"

[[bench]]
name = "hamt_beckmark"
Expand Down
2 changes: 1 addition & 1 deletion ipld/hamt/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use forest_encoding::{
};
use std::u64;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub struct Bitfield([u64; 4]);

impl Serialize for Bitfield {
Expand Down
23 changes: 10 additions & 13 deletions ipld/hamt/src/hamt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use std::marker::PhantomData;
///
/// let mut map: Hamt<_, _, usize> = Hamt::new(&store);
/// map.set(1, "a".to_string()).unwrap();
/// assert_eq!(map.get(&1).unwrap(), Some("a".to_string()));
/// assert_eq!(map.delete(&1).unwrap(), true);
/// assert_eq!(map.get(&1).unwrap(), Some(&"a".to_string()));
/// assert_eq!(map.delete(&1).unwrap(), Some((1, "a".to_string())));
/// assert_eq!(map.get::<_>(&1).unwrap(), None);
/// let cid = map.flush().unwrap();
/// ```
Expand Down Expand Up @@ -60,8 +60,8 @@ impl<'a, K: PartialEq, V: PartialEq, S: BlockStore, H: HashAlgorithm> PartialEq

impl<'a, BS, V, K, H> Hamt<'a, BS, V, K, H>
where
K: Hash + Eq + PartialOrd + Serialize + DeserializeOwned + Clone,
V: Serialize + DeserializeOwned + Clone,
K: Hash + Eq + PartialOrd + Serialize + DeserializeOwned,
V: Serialize + DeserializeOwned,
BS: BlockStore,
H: HashAlgorithm,
{
Expand Down Expand Up @@ -152,11 +152,11 @@ where
///
/// let mut map: Hamt<_, _, usize> = Hamt::new(&store);
/// map.set(1, "a".to_string()).unwrap();
/// assert_eq!(map.get(&1).unwrap(), Some("a".to_string()));
/// assert_eq!(map.get(&1).unwrap(), Some(&"a".to_string()));
/// assert_eq!(map.get(&2).unwrap(), None);
/// ```
#[inline]
pub fn get<Q: ?Sized>(&self, k: &Q) -> Result<Option<V>, Error>
pub fn get<Q: ?Sized>(&self, k: &Q) -> Result<Option<&V>, Error>
where
K: Borrow<Q>,
Q: Hash + Eq,
Expand Down Expand Up @@ -211,18 +211,15 @@ where
///
/// let mut map: Hamt<_, _, usize> = Hamt::new(&store);
/// map.set(1, "a".to_string()).unwrap();
/// assert_eq!(map.delete(&1).unwrap(), true);
/// assert_eq!(map.delete(&1).unwrap(), false);
/// assert_eq!(map.delete(&1).unwrap(), Some((1, "a".to_string())));
/// assert_eq!(map.delete(&1).unwrap(), None);
/// ```
pub fn delete<Q: ?Sized>(&mut self, k: &Q) -> Result<bool, Error>
pub fn delete<Q: ?Sized>(&mut self, k: &Q) -> Result<Option<(K, V)>, Error>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
match self.root.remove_entry(k, self.store, self.bit_width)? {
Some(_) => Ok(true),
None => Ok(false),
}
self.root.remove_entry(k, self.store, self.bit_width)
}

/// Flush root and return Cid for hamt
Expand Down
22 changes: 0 additions & 22 deletions ipld/hamt/src/hash_algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
use crate::{Hash, HashedKey};
use sha2::{Digest, Sha256 as Sha256Hasher};

#[cfg(feature = "murmur")]
use murmur3::murmur3_x64_128::MurmurHasher;

/// Algorithm used as the hasher for the Hamt.
pub trait HashAlgorithm {
fn hash<X: ?Sized>(key: &X) -> HashedKey
Expand Down Expand Up @@ -43,25 +40,6 @@ impl HashAlgorithm for Sha256 {
}
}

#[cfg(feature = "murmur")]
#[derive(Debug)]
pub enum Murmur3 {}

#[cfg(feature = "murmur")]
impl HashAlgorithm for Murmur3 {
fn hash<X: ?Sized>(key: &X) -> HashedKey
where
X: Hash,
{
let mut hasher = MurmurHasher::default();
key.hash(&mut hasher);
let mut bz: [u8; 32] = Default::default();
let hash: [u8; 16] = hasher.finalize().into();
bz[0..16].copy_from_slice(&hash);
bz
}
}

#[cfg(feature = "identity")]
use std::hash::Hasher;

Expand Down
2 changes: 1 addition & 1 deletion ipld/hamt/src/hash_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{Error, HashedKey};
use std::cmp::Ordering;

/// Helper struct which indexes and allows returning bits from a hashed key
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct HashBits<'a> {
b: &'a HashedKey,
pub consumed: u32,
Expand Down
5 changes: 4 additions & 1 deletion ipld/hamt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ const DEFAULT_BIT_WIDTH: u32 = 8;

type HashedKey = [u8; 32];

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct KeyValuePair<K, V>(K, V);

impl<K, V> KeyValuePair<K, V> {
pub fn key(&self) -> &K {
&self.0
}
pub fn value(&self) -> &V {
&self.1
}
}

impl<K, V> KeyValuePair<K, V> {
Expand Down
Loading