refactor: remove lazy_static
once_cell has now been merged into rust core. This removes the
lazy_static dependency and migrates over to the built in `OnceLock`.
Its always good to remove dependencies where possible, this also give us
a preference for the built in `OnceLock`
```
Benchmark 1: chore: add benchmark for `set_items`
Time (mean ± σ): 6.327 s ± 0.199 s [User: 15.316 s, System: 1.323 s]
Range (min … max): 6.087 s … 6.712 s 10 runs
Benchmark 2: refactor: remove lazy_static
Time (mean ± σ): 6.171 s ± 0.251 s [User: 15.223 s, System: 1.382 s]
Range (min … max): 5.910 s … 6.776 s 10 runs
Summary
'refactor: remove lazy_static' ran
1.03 ± 0.05 times faster than 'chore: add benchmark for `set_items`'
```
This commit is contained in:
parent
6af2b5011b
commit
c562190829
3 changed files with 22 additions and 10 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -341,7 +341,6 @@ dependencies = [
|
||||||
"criterion",
|
"criterion",
|
||||||
"fuzzy-matcher",
|
"fuzzy-matcher",
|
||||||
"ignore",
|
"ignore",
|
||||||
"lazy_static",
|
|
||||||
"rayon",
|
"rayon",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ path = "rust/lib.rs"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ignore = "0.4.20"
|
ignore = "0.4.20"
|
||||||
fuzzy-matcher = "0.3.7"
|
fuzzy-matcher = "0.3.7"
|
||||||
lazy_static = "1.4.0"
|
|
||||||
rayon = "1.7.0"
|
rayon = "1.7.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
||||||
30
rust/lib.rs
30
rust/lib.rs
|
|
@ -7,12 +7,24 @@ use std::ffi::CStr;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::os::raw::{c_char, c_int};
|
use std::os::raw::{c_char, c_int};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
#[macro_use]
|
struct Ivy {
|
||||||
extern crate lazy_static;
|
pub file_cache: HashMap<String, Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
lazy_static! {
|
static INSTANCE: OnceLock<Mutex<Ivy>> = OnceLock::new();
|
||||||
static ref GLOBAL_FILE_CACHE: Mutex<HashMap<String, Vec<String>>> = Mutex::new(HashMap::new());
|
|
||||||
|
impl Ivy {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
file_cache: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn global() -> &'static Mutex<Ivy> {
|
||||||
|
INSTANCE.get_or_init(|| Mutex::new(Ivy::new()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_string(input: *const c_char) -> String {
|
fn to_string(input: *const c_char) -> String {
|
||||||
|
|
@ -23,15 +35,17 @@ fn to_string(input: *const c_char) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_files(directory: &String) -> Vec<String> {
|
fn get_files(directory: &String) -> Vec<String> {
|
||||||
let mut cache = GLOBAL_FILE_CACHE.lock().unwrap();
|
let mut ivy = Ivy::global().lock().unwrap();
|
||||||
if !cache.contains_key(directory) {
|
if !ivy.file_cache.contains_key(directory) {
|
||||||
let finder_options = finder::Options {
|
let finder_options = finder::Options {
|
||||||
directory: directory.clone(),
|
directory: directory.clone(),
|
||||||
};
|
};
|
||||||
cache.insert(directory.clone(), finder::find_files(finder_options));
|
|
||||||
|
ivy.file_cache
|
||||||
|
.insert(directory.clone(), finder::find_files(finder_options));
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.get(directory).unwrap().to_vec();
|
return ivy.file_cache.get(directory).unwrap().to_vec();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue