From 7fba62fc229433536d671b9f493d6a73eaf0d3d9 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Sat, 5 May 2018 13:50:51 +0200 Subject: [PATCH] feat: Remove lazy_static's sync overhead --- Cargo.lock | 1 - Cargo.toml | 1 - src/bin/raptor.rs | 48 +++++++++++++++++++++++------------------------ src/fst_map.rs | 2 +- src/lib.rs | 16 +--------------- 5 files changed, 26 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c142b8cc7..680034b97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", diff --git a/Cargo.toml b/Cargo.toml index 9f926679b..1aafa05d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/bin/raptor.rs b/src/bin/raptor.rs index 772c89f5f..20a285409 100644 --- a/src/bin/raptor.rs +++ b/src/bin/raptor.rs @@ -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 = { - 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> = None; +static mut LEV_AUT_BLDR_0: Option = None; +static mut LEV_AUT_BLDR_1: Option = None; +static mut LEV_AUT_BLDR_2: Option = None; struct MainService { map: &'static FstMap, @@ -109,18 +100,27 @@ fn read_to_vec>(path: P) -> io::Result> { 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(), + })) + } } diff --git a/src/fst_map.rs b/src/fst_map.rs index 3ffda0be8..42b77e115 100644 --- a/src/fst_map.rs +++ b/src/fst_map.rs @@ -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}; diff --git a/src/lib.rs b/src/lib.rs index 80f34184c..afda8bd7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, } -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]);