feat: Remove lazy_static's sync overhead

This commit is contained in:
Kerollmops 2018-05-05 13:50:51 +02:00 committed by Clément Renault
parent 2ffb140963
commit 7fba62fc22
5 changed files with 26 additions and 42 deletions

1
Cargo.lock generated
View File

@ -307,7 +307,6 @@ dependencies = [
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"fst 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"levenshtein_automata 0.1.0 (git+https://github.com/tantivy-search/levenshtein-automata.git)",
"serde 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -9,7 +9,6 @@ env_logger = { version = "0.3", default-features = false }
fst = "0.3"
levenshtein_automata = { git = "https://github.com/tantivy-search/levenshtein-automata.git", features = ["fst_automaton"] }
futures = "0.1"
lazy_static = "1.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

View File

@ -1,4 +1,3 @@
#[macro_use] extern crate lazy_static;
extern crate env_logger;
extern crate fst;
extern crate futures;
@ -23,18 +22,10 @@ use tokio_service::Service;
use raptor::FstMap;
lazy_static! {
static ref MAP: FstMap<u64> = {
let map = read_to_vec("map.fst").unwrap();
let values = read_to_vec("values.vecs").unwrap();
FstMap::from_bytes(map, &values).unwrap()
};
static ref LEV_AUT_BLDR_0: LevenshteinAutomatonBuilder = LevenshteinAutomatonBuilder::new(0, false);
static ref LEV_AUT_BLDR_1: LevenshteinAutomatonBuilder = LevenshteinAutomatonBuilder::new(1, false);
static ref LEV_AUT_BLDR_2: LevenshteinAutomatonBuilder = LevenshteinAutomatonBuilder::new(2, false);
}
static mut MAP: Option<FstMap<u64>> = None;
static mut LEV_AUT_BLDR_0: Option<LevenshteinAutomatonBuilder> = None;
static mut LEV_AUT_BLDR_1: Option<LevenshteinAutomatonBuilder> = None;
static mut LEV_AUT_BLDR_2: Option<LevenshteinAutomatonBuilder> = None;
struct MainService {
map: &'static FstMap<u64>,
@ -109,18 +100,27 @@ fn read_to_vec<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
fn main() {
drop(env_logger::init());
// initialize all "lazy" variables
lazy_static::initialize(&MAP);
lazy_static::initialize(&LEV_AUT_BLDR_0);
lazy_static::initialize(&LEV_AUT_BLDR_1);
lazy_static::initialize(&LEV_AUT_BLDR_2);
// initialize all static variables
unsafe {
MAP = {
let map = read_to_vec("map.fst").unwrap();
let values = read_to_vec("values.vecs").unwrap();
Some(FstMap::from_bytes(map, &values).unwrap())
};
LEV_AUT_BLDR_0 = Some(LevenshteinAutomatonBuilder::new(0, false));
LEV_AUT_BLDR_1 = Some(LevenshteinAutomatonBuilder::new(1, false));
LEV_AUT_BLDR_2 = Some(LevenshteinAutomatonBuilder::new(2, false));
}
let addr = "0.0.0.0:8080".parse().unwrap();
TcpServer::new(Http, addr).serve(|| Ok(MainService {
map: &MAP,
lev_aut_bldr_0: &LEV_AUT_BLDR_0,
lev_aut_bldr_1: &LEV_AUT_BLDR_1,
lev_aut_bldr_2: &LEV_AUT_BLDR_2,
}))
unsafe {
TcpServer::new(Http, addr).serve(|| Ok(MainService {
map: MAP.as_ref().unwrap(),
lev_aut_bldr_0: LEV_AUT_BLDR_0.as_ref().unwrap(),
lev_aut_bldr_1: LEV_AUT_BLDR_1.as_ref().unwrap(),
lev_aut_bldr_2: LEV_AUT_BLDR_2.as_ref().unwrap(),
}))
}
}

View File

@ -4,7 +4,7 @@ use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use std::fs::File;
use std::io::{Write, BufReader};
use std::ops::{Range, Deref, DerefMut};
use std::ops::Range;
use std::path::Path;
use {StreamBuilder, Stream};

View File

@ -5,7 +5,7 @@ extern crate serde;
mod fst_map;
use std::ops::{Range, Deref, DerefMut};
use std::ops::Range;
use std::io::{Write, BufReader};
use std::fs::File;
use std::path::Path;
@ -20,20 +20,6 @@ pub struct StreamBuilder<'a, T: 'a, A: Automaton> {
values: &'a Values<T>,
}
impl<'a, T, A: Automaton> Deref for StreamBuilder<'a, T, A> {
type Target = fst::map::StreamBuilder<'a, A>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'a, T, A: Automaton> DerefMut for StreamBuilder<'a, T, A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<'a, T: 'a, A: Automaton> fst::IntoStreamer<'a> for StreamBuilder<'a, T, A> {
type Item = (&'a str, &'a [T]);