mirror of
https://github.com/vacp2p/pmtree.git
synced 2026-01-08 23:08:02 -05:00
feat: bump rayon version, parallel feature flag (#4)
* feat: bump rayon version * feat: parallel feature flag * chore: bump pmtree minor version number
This commit is contained in:
12
Cargo.toml
12
Cargo.toml
@@ -1,15 +1,19 @@
|
||||
[package]
|
||||
name = "vacp2p_pmtree"
|
||||
version = "2.0.2"
|
||||
version = "2.0.3"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "Persistent Merkle Tree in Rust"
|
||||
|
||||
[dev-dependencies]
|
||||
hex-literal = "=0.3.4"
|
||||
hex-literal = "0.3.4"
|
||||
tiny-keccak = { version = "=2.0.2", features = ["keccak"] }
|
||||
sled = "=0.34.7"
|
||||
ark-serialize = "=0.3.0"
|
||||
ark-serialize = { version = "0.5.0", default-features = false }
|
||||
|
||||
[dependencies]
|
||||
rayon = { version = "=1.7.0", optional = false }
|
||||
rayon = { version = "1.10.0", optional = true }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
parallel = ["rayon", "ark-serialize/parallel"]
|
||||
|
||||
16
src/tree.rs
16
src/tree.rs
@@ -4,6 +4,9 @@ use std::cmp::{max, min};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[cfg(feature = "parallel")]
|
||||
use rayon;
|
||||
|
||||
// db[DEPTH_KEY] = depth
|
||||
const DEPTH_KEY: DBKey = (u64::MAX - 1).to_be_bytes();
|
||||
|
||||
@@ -240,11 +243,7 @@ where
|
||||
|
||||
let subtree = Arc::new(RwLock::new(subtree));
|
||||
|
||||
let root_val = rayon::ThreadPoolBuilder::new()
|
||||
.num_threads(rayon::current_num_threads())
|
||||
.build()
|
||||
.unwrap()
|
||||
.install(|| Self::batch_recalculate(root_key, Arc::clone(&subtree), self.depth));
|
||||
let root_val = Self::batch_recalculate(root_key, Arc::clone(&subtree), self.depth);
|
||||
|
||||
let subtree = RwLock::into_inner(Arc::try_unwrap(subtree).unwrap()).unwrap();
|
||||
|
||||
@@ -320,11 +319,18 @@ where
|
||||
return *subtree.read().unwrap().get(&key).unwrap();
|
||||
}
|
||||
|
||||
#[cfg(feature = "parallel")]
|
||||
let (left, right) = rayon::join(
|
||||
|| Self::batch_recalculate(left_child, Arc::clone(&subtree), depth),
|
||||
|| Self::batch_recalculate(right_child, Arc::clone(&subtree), depth),
|
||||
);
|
||||
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
let (left, right) = (
|
||||
Self::batch_recalculate(left_child, Arc::clone(&subtree), depth),
|
||||
Self::batch_recalculate(right_child, Arc::clone(&subtree), depth),
|
||||
);
|
||||
|
||||
let result = H::hash(&[left, right]);
|
||||
|
||||
subtree.write().unwrap().insert(key, result);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use hex_literal::hex;
|
||||
use vacp2p_pmtree::*;
|
||||
use std::collections::HashMap;
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
use vacp2p_pmtree::*;
|
||||
|
||||
struct MemoryDB(HashMap<DBKey, Value>);
|
||||
struct MyKeccak(Keccak);
|
||||
struct MyKeccak;
|
||||
|
||||
#[derive(Default)]
|
||||
struct MemoryDBConfig;
|
||||
@@ -33,7 +33,7 @@ impl Database for MemoryDB {
|
||||
}
|
||||
|
||||
fn put_batch(&mut self, subtree: HashMap<DBKey, Value>) -> PmtreeResult<()> {
|
||||
self.0.extend(subtree.into_iter());
|
||||
self.0.extend(subtree);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use hex_literal::hex;
|
||||
use vacp2p_pmtree::*;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
use vacp2p_pmtree::*;
|
||||
|
||||
struct MyKeccak(Keccak);
|
||||
struct MyKeccak;
|
||||
struct MySled(sled::Db);
|
||||
|
||||
#[derive(Default)]
|
||||
|
||||
Reference in New Issue
Block a user