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:
Ade Attwood 2023-10-22 16:14:14 +01:00
parent 6af2b5011b
commit c562190829
3 changed files with 22 additions and 10 deletions

1
Cargo.lock generated
View file

@ -341,7 +341,6 @@ dependencies = [
"criterion",
"fuzzy-matcher",
"ignore",
"lazy_static",
"rayon",
]

View file

@ -11,7 +11,6 @@ path = "rust/lib.rs"
[dependencies]
ignore = "0.4.20"
fuzzy-matcher = "0.3.7"
lazy_static = "1.4.0"
rayon = "1.7.0"
[dev-dependencies]

View file

@ -7,12 +7,24 @@ use std::ffi::CStr;
use std::ffi::CString;
use std::os::raw::{c_char, c_int};
use std::sync::Mutex;
use std::sync::OnceLock;
#[macro_use]
extern crate lazy_static;
struct Ivy {
pub file_cache: HashMap<String, Vec<String>>,
}
lazy_static! {
static ref GLOBAL_FILE_CACHE: Mutex<HashMap<String, Vec<String>>> = Mutex::new(HashMap::new());
static INSTANCE: OnceLock<Mutex<Ivy>> = OnceLock::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 {
@ -23,15 +35,17 @@ fn to_string(input: *const c_char) -> String {
}
fn get_files(directory: &String) -> Vec<String> {
let mut cache = GLOBAL_FILE_CACHE.lock().unwrap();
if !cache.contains_key(directory) {
let mut ivy = Ivy::global().lock().unwrap();
if !ivy.file_cache.contains_key(directory) {
let finder_options = finder::Options {
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]