Skip to content

Commit

Permalink
use once_cell instead of lazy_static and lazy-init (#815)
Browse files Browse the repository at this point in the history
* replace lazy_static and lazy_init with once_cell
* remove sync bounds
* use builder methods
  • Loading branch information
luizirber authored Dec 29, 2019
1 parent 448be5d commit 3e71e9a
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 191 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ failure = "0.1.6"
failure_derive = "0.1.6"
finch = { version = "0.3.0", optional = true }
fixedbitset = "0.2.0"
lazy_static = "1.4.0"
lazy-init = "0.3.0"
log = "0.4.8"
md5 = "0.7.0"
murmurhash3 = "0.0.5"
once_cell = "1.2.0"
serde = "1.0.103"
serde_derive = "1.0.103"
serde_json = "1.0.44"
Expand Down
4 changes: 2 additions & 2 deletions src/index/bigsi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ mod test {
let results_sbt = sbt.search(&leaf, 0.5, false).unwrap();
assert_eq!(results_sbt.len(), 1);

let data = (*leaf.data).get().unwrap();
let data = leaf.data.get().unwrap();
let results_bigsi = bigsi.search(&data, 0.5, false).unwrap();
assert_eq!(results_bigsi.len(), 1);

Expand All @@ -207,7 +207,7 @@ mod test {
let results_sbt = sbt.search(&leaf, 0.1, false).unwrap();
assert_eq!(results_sbt.len(), 2);

let data = (*leaf.data).get().unwrap();
let data = leaf.data.get().unwrap();
let results_bigsi = bigsi.search(&data, 0.1, false).unwrap();
assert_eq!(results_bigsi.len(), 2);

Expand Down
20 changes: 7 additions & 13 deletions src/index/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ use std::path::PathBuf;
use std::rc::Rc;

use failure::Error;
use lazy_init::Lazy;
use serde_derive::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::index::storage::{FSStorage, ReadData, Storage, StorageInfo, ToWriter};
use crate::index::{Comparable, DatasetInfo, Index, SigStore};

#[derive(TypedBuilder)]
pub struct LinearIndex<L>
where
L: Sync,
{
pub struct LinearIndex<L> {
#[builder(default)]
storage: Option<Rc<dyn Storage>>,

Expand All @@ -34,7 +30,7 @@ struct LinearInfo<L> {

impl<'a, L> Index<'a> for LinearIndex<L>
where
L: Sync + Clone + Comparable<L> + 'a,
L: Clone + Comparable<L> + 'a,
SigStore<L>: From<L>,
{
type Item = L;
Expand Down Expand Up @@ -83,7 +79,7 @@ where

impl<L> LinearIndex<L>
where
L: std::marker::Sync + ToWriter,
L: ToWriter,
SigStore<L>: ReadData<L>,
{
pub fn save_file<P: AsRef<Path>>(
Expand Down Expand Up @@ -175,12 +171,10 @@ where
datasets: linear
.leaves
.into_iter()
.map(|l| SigStore {
filename: l.filename,
name: l.name,
metadata: l.metadata,
storage: Some(Rc::clone(&storage)),
data: Rc::new(Lazy::new()),
.map(|l| {
let mut v: SigStore<L> = l.into();
v.storage = Some(Rc::clone(&storage));
v
})
.collect(),
})
Expand Down
39 changes: 20 additions & 19 deletions src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::path::Path;
use std::rc::Rc;

use failure::Error;
use lazy_init::Lazy;
use once_cell::sync::OnceCell;
use serde_derive::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -135,32 +135,24 @@ pub struct DatasetInfo {
}

#[derive(TypedBuilder, Default, Clone)]
pub struct SigStore<T>
where
T: std::marker::Sync,
{
pub struct SigStore<T> {
pub(crate) filename: String,
pub(crate) name: String,
pub(crate) metadata: String,

pub(crate) storage: Option<Rc<dyn Storage>>,

pub(crate) data: Rc<Lazy<T>>,
#[builder(default)]
pub(crate) data: OnceCell<T>,
}

impl<T> SigStore<T>
where
T: std::marker::Sync + Default,
{
impl<T> SigStore<T> {
pub fn name(&self) -> String {
self.name.clone()
}
}

impl<T> std::fmt::Debug for SigStore<T>
where
T: std::marker::Sync,
{
impl<T> std::fmt::Debug for SigStore<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand All @@ -175,7 +167,7 @@ impl ReadData<Signature> for SigStore<Signature> {
if let Some(sig) = self.data.get() {
Ok(sig)
} else if let Some(storage) = &self.storage {
let sig = self.data.get_or_create(|| {
let sig = self.data.get_or_init(|| {
let raw = storage.load(&self.filename).unwrap();
let sigs: Result<Vec<Signature>, _> = serde_json::from_reader(&mut &raw[..]);
if let Ok(sigs) = sigs {
Expand Down Expand Up @@ -241,13 +233,10 @@ impl From<Signature> for SigStore<Signature> {
let name = other.name();
let filename = other.filename();

let data = Lazy::new();
data.get_or_create(|| other);

SigStore::builder()
.name(name)
.filename(filename)
.data(data)
.data(other)
.metadata("")
.storage(None)
.build()
Expand Down Expand Up @@ -329,3 +318,15 @@ impl Comparable<Signature> for Signature {
unimplemented!()
}
}

impl<L> From<DatasetInfo> for SigStore<L> {
fn from(other: DatasetInfo) -> SigStore<L> {
SigStore {
filename: other.filename,
name: other.name,
metadata: other.metadata,
storage: None,
data: OnceCell::new(),
}
}
}
28 changes: 5 additions & 23 deletions src/index/sbt/mhbt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::collections::HashMap;
use std::io::Write;
use std::rc::Rc;

use failure::Error;
use lazy_init::Lazy;

use crate::index::sbt::{Factory, FromFactory, Node, Update, SBT};
use crate::index::storage::{ReadData, ReadDataError, ToWriter};
Expand All @@ -21,21 +19,18 @@ impl ToWriter for Nodegraph {
}
}

impl<L: Sync + Clone + Default> FromFactory<Node<Nodegraph>> for SBT<Node<Nodegraph>, L> {
impl<L: Clone + Default> FromFactory<Node<Nodegraph>> for SBT<Node<Nodegraph>, L> {
fn factory(&self, name: &str) -> Result<Node<Nodegraph>, Error> {
match self.factory {
Factory::GraphFactory { args: (k, t, n) } => {
let n = Nodegraph::with_tables(t as usize, n as usize, k as usize);

let data = Lazy::new();
data.get_or_create(|| n);

Ok(Node::builder()
.filename(name)
.name(name)
.metadata(HashMap::default())
.storage(self.storage())
.data(Rc::new(data))
.data(n)
.build())
}
}
Expand Down Expand Up @@ -72,9 +67,7 @@ impl Update<Node<Nodegraph>> for Signature {
unimplemented!()
}

let data = Lazy::new();
data.get_or_create(|| parent_data);
parent.data = Rc::new(data);
parent.data = parent_data.into();

Ok(())
}
Expand Down Expand Up @@ -139,7 +132,7 @@ impl Comparable<Signature> for Node<Nodegraph> {
impl ReadData<Nodegraph> for Node<Nodegraph> {
fn data(&self) -> Result<&Nodegraph, Error> {
if let Some(storage) = &self.storage {
Ok(self.data.get_or_create(|| {
Ok(self.data.get_or_init(|| {
let raw = storage.load(&self.filename).unwrap();
Nodegraph::from_reader(&mut &raw[..]).unwrap()
}))
Expand All @@ -157,10 +150,8 @@ mod test {
use std::fs::File;
use std::io::{BufReader, Seek, SeekFrom};
use std::path::PathBuf;
use std::rc::Rc;

use assert_matches::assert_matches;
use lazy_init::Lazy;
use tempfile;

use super::Factory;
Expand Down Expand Up @@ -217,16 +208,7 @@ mod test {
.unwrap();
let sig_data = sigs[0].clone();

let data = Lazy::new();
data.get_or_create(|| sig_data);

let leaf = SigStore::builder()
.data(Rc::new(data))
.filename("")
.name("")
.metadata("")
.storage(None)
.build();
let leaf = sig_data.into();

let results = sbt.find(search_minhashes, &leaf, 0.5).unwrap();
assert_eq!(results.len(), 1);
Expand Down
Loading

0 comments on commit 3e71e9a

Please sign in to comment.