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)", "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)", "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)", "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)", "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 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)", "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" fst = "0.3"
levenshtein_automata = { git = "https://github.com/tantivy-search/levenshtein-automata.git", features = ["fst_automaton"] } levenshtein_automata = { git = "https://github.com/tantivy-search/levenshtein-automata.git", features = ["fst_automaton"] }
futures = "0.1" futures = "0.1"
lazy_static = "1.0"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"

View File

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

View File

@ -5,7 +5,7 @@ extern crate serde;
mod fst_map; mod fst_map;
use std::ops::{Range, Deref, DerefMut}; use std::ops::Range;
use std::io::{Write, BufReader}; use std::io::{Write, BufReader};
use std::fs::File; use std::fs::File;
use std::path::Path; use std::path::Path;
@ -20,20 +20,6 @@ pub struct StreamBuilder<'a, T: 'a, A: Automaton> {
values: &'a Values<T>, 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> { impl<'a, T: 'a, A: Automaton> fst::IntoStreamer<'a> for StreamBuilder<'a, T, A> {
type Item = (&'a str, &'a [T]); type Item = (&'a str, &'a [T]);